Created
March 22, 2017 13:34
-
-
Save judy2k/7656bfe3b322d669ef75364a46327836 to your computer and use it in GitHub Desktop.
Parse a .env (dotenv) file directly using BASH
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
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
The solution proposed by @arizonaherbaltea will not work correctly when the file is not terminated with a newline. For example using this
.env
example,Will not work properly, whereas the following will,
The only change is adding a newline at the end of the file. This is because
read
requires a newline to parse the line correctly. Thus, in order to ensure everything works properly we can add a check to see if the file ends with newline and if not append it before parsing it. Doing this will ensure all lines are parsed correctly.Then it results,
Hope this helps someone out there :)