Skip to content

Instantly share code, notes, and snippets.

@judy2k
Created March 22, 2017 13:34
Show Gist options
  • Save judy2k/7656bfe3b322d669ef75364a46327836 to your computer and use it in GitHub Desktop.
Save judy2k/7656bfe3b322d669ef75364a46327836 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)
@carlosonunez
Copy link

carlosonunez commented Dec 25, 2024

@tgrushka's method is the most reliable one for me when used in scripts.

You can also use it like this if your env vars are in a string:

set -o allexport
source <(grep -Ev '^#' <<< "$YOUR_ENV_VAR_STRING")
# rest of the script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment