Skip to content

Instantly share code, notes, and snippets.

@WRonX
Created September 30, 2022 12:19
Show Gist options
  • Save WRonX/9545fe1e75eec9733e25ea1b1e5abd83 to your computer and use it in GitHub Desktop.
Save WRonX/9545fe1e75eec9733e25ea1b1e5abd83 to your computer and use it in GitHub Desktop.
Shell script to export prefixed environment variables to JSON file
#!/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