Skip to content

Instantly share code, notes, and snippets.

@tinker1987
Forked from judy2k/parse_dotenv.bash
Last active November 25, 2019 07:51
Show Gist options
  • Save tinker1987/a9382ab3561283645dd70405982550b5 to your computer and use it in GitHub Desktop.
Save tinker1987/a9382ab3561283645dd70405982550b5 to your computer and use it in GitHub Desktop.
Parse a .env (dotenv) file directly using BASH
# 1. Pass the env-vars to MYCOMMAND
eval $(egrep -v '^#' .env | xargs -d '\n') MYCOMMAND
# … or ...
# 2. Export the vars in .env into your shell:
export $(egrep -v '^#' .env | xargs -d '\n')
# 3. Avoid existent vars overriding
source <(grep -v '^#' .env | sed -E 's|^(.+)=(.*)$|: ${\1=\2}; export \1|g')
# 4.
set -o allexport
[[ -f .env ]] && source .env
set +o allexport
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment