Skip to content

Instantly share code, notes, and snippets.

@DominicImhof
Last active May 20, 2016 11:38
Show Gist options
  • Save DominicImhof/7d457b0a023e068b7d58104fe87ce355 to your computer and use it in GitHub Desktop.
Save DominicImhof/7d457b0a023e068b7d58104fe87ce355 to your computer and use it in GitHub Desktop.
set variable in a .env file
PLATFORM=$(uname)
KEY=$1
VAL=$2
# Use awk instead of grep, since grep is not able to handle multiline regex
if [ "$(awk "/^$KEY=.*$/" .env)" != "" ]; then
if [ "$PLATFORM" = "Darwin" ]; then
sed -i '' -E "s~$KEY=.*~$KEY=$VAL~g" .env
else
sed -i "s~$KEY=.*~$KEY=$VAL~g" .env
fi
else
echo "$KEY=$VAL" >> .env
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment