Skip to content

Instantly share code, notes, and snippets.

@rochacbruno
Forked from judy2k/parse_dotenv.bash
Last active March 16, 2021 16:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rochacbruno/016b344c970c80871a4ceb810f42161b to your computer and use it in GitHub Desktop.
Save rochacbruno/016b344c970c80871a4ceb810f42161b to your computer and use it in GitHub Desktop.
Parse a .env (dotenv) file directly using BASH
# Pass the env-vars to MYCOMMAND
eval $(egrep -v '^#' .env | xargs) MYCOMMAND
# … or ...
# Export the vars in .env into your shell:
export $(egrep -v '^#' .env | xargs)
# set variables from .compose.env but don't override existing exported vars
eval "$(grep -v '^#' .compose.env | sed -E 's|^(.+)=(.*)$|export \1=${\1:-\2}|g' | xargs -L 1)"
# Load up .env
set -o allexport
[[ -f .env ]] && source .env
set +o allexport
# or
set -a
[ -f .env ] && . .env
set +a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment