Created
September 30, 2022 12:19
-
-
Save WRonX/9545fe1e75eec9733e25ea1b1e5abd83 to your computer and use it in GitHub Desktop.
Shell script to export prefixed environment variables to JSON file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
ENV_FILE="env.json" | |
ENV_PREFIX="REACT_APP_" | |
shopt -s lastpipe | |
FOUND=0 | |
echo '{' > $ENV_FILE | |
env | while IFS= read -r line; do | |
value=${line#*=} | |
name=${line%%=*} | |
if [[ "$name" == "$ENV_PREFIX"* ]] | |
then | |
if [[ $FOUND == 1 ]] | |
then | |
echo "$(cat $ENV_FILE)," > $ENV_FILE | |
fi | |
echo " \"$name\": \"$value\"" >> $ENV_FILE | |
FOUND=1 | |
fi | |
done | |
echo '}' >> $ENV_FILE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment