Load environment variables from dotenv / .env file in 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
if [ ! -f .env ] | |
then | |
export $(cat .env | xargs) | |
fi |
When the values have newline chars \n
, spaces or quotes, it can get messy.
After a lot of trial and error, I ended up with a variation of what @bergkvist proposed in https://gist.github.com/mihow/9c7f559807069a03e302605691f85572?permalink_comment_id=4245050#gistcomment-4245050 (thank you very much!).
ENV_VARS="$(cat .env | awk '!/^\s*#/' | awk '!/^\s*$/')"
eval "$(
printf '%s\n' "$ENV_VARS" | while IFS='' read -r line; do
key=$(printf '%s\n' "$line"| sed 's/"/\\"/g' | cut -d '=' -f 1)
value=$(printf '%s\n' "$line" | cut -d '=' -f 2- | sed 's/"/\\\"/g')
printf '%s\n' "export $key=\"$value\""
done
)"
env $(cat .env)
this does not work for me but this one works
env $(cat .env|xargs) CMD
my .env has some special value such as FOO='VPTO&wH7$^3ZHZX$o$udY4&i'
@NatoBoram
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Posix compliant version built around
set
,[ ]
and.
Many thanks to the prior posters who brought upset -o a
andset -a
/set +a
This snippet will source a dotenv file, exporting the values into the environment. If
allexport
is already set, it leaves it set, otherwise it sets, reads, and unsets.double brackets
[[
,source
,setopt
are not available in posix. Nor is the test[[ -o a ]]
to check for set options. And we need to quote our comparison strings to deal with empty vars.The code to check if an option is set is a bit of a pain. It could be a case statement or a grep on
set -o
likeset -o | grep allexport | grep -q yes
, but blech. Instead I've used parameter expansion with pattern matching to remove a maximum match from the$-
variable containing a single line of the set options.${-%%*a*}
uses%%
parameter expansion to remove the longest suffix matching the pattern*a*
. If$-
containsa
then this expansion produces and empty string which we can test with -z or -n.subtle bug if no options are set, so the comparison
"$-" = "${-%%a*}"
will check that the expansion changed the string.allexport
is set if the two strings differ. And even % will work as we don't need a maximal match and can remove the leading*
from our pattern match.