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)
@gmeligio
Copy link

I've been using shdotenv from @ko1nksm and it's been great.

You can do shdotenv my-command to parse and run a command.
If you want to export the variables to the shell session, use eval $(shdotenv).

@tgrushka
Copy link

tgrushka commented Feb 6, 2024

What's wrong with:

if [ -f .env ]; then
    set -o allexport
    source .env
fi

Works on macOS with my .env that works with docker compose and does not have quotes around every string.

Test with:

envsubst < "$secret_file" | cat

later in the same script.

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