Load environment variables from dotenv / .env file in Bash
source link: https://gist.github.com/mihow/9c7f559807069a03e302605691f85572
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Bioblaze commented on Jun 6, 2021 •
# Local .env
if [ -f .env ]; then
# Load Environment Variables
export $(cat .env | grep -v '#' | sed 's/\r$//' | awk '/=/ {print $1}' )
fi
is what I use.. <.< it allows me to comment inside of the .env file :X and handles /r proper.. hope that helps everyone <3
Here is a modified version of this code that allows for variable expansion
if [ -f .env ]; then export $(echo $(cat .env | sed 's/#.*//g'| xargs) | envsubst) fi
This worked for me. Thanks
Went for:
loadEnv() {
local envFile="${1?Missing environment file}"
local environmentAsArray variableDeclaration
mapfile environmentAsArray < <(
grep --invert-match '^#' "${envFile}" \
| grep --invert-match '^\s*$'
) # Uses grep to remove commented and blank lines
for variableDeclaration in "${environmentAsArray[@]}"; do
export "${variableDeclaration//[$'\r\n']}" # The substitution removes the line breaks
done
}
It does not glob or swallow quotes, tested with:
WITH_QUOTES=''
# Comment
WITNESS=whatever
WITH_GLOB=*.sh
In this folder (so the glob matches the shell file):
.
├── .env
└── loadEnvTest.sh
With command:
source ./loadEnvTest.sh; loadEnv .env; echo "$WITH_QUOTES $WITH_GLOB"
Outputs:
'' *.sh
darbe habercisi
One I use (bash specific) which behaves correctly in presence of calling environment override:
. <(sed -n 's/^\([^#][^=]*\)=\(.*\)$/\1=${\1:-\2}/p' .env 2>/dev/null) || true
if [ ! -f .env ]
then
export $(cat .env | xargs)
fi
I hate bash so much...... it took me 30 minutes to understand it should be if [ -f .env ]
instead of if [ ! -f .env ]
chengxuncc commented on Aug 26, 2021 •
[ ! -f .env ] || export $(sed 's/#.*//g' .env | xargs)
Update:TEXT="abc#def"
not work as expected, so just replace line begin with #.
[ ! -f .env ] || export $(grep -v '^#' .env | xargs)
ShivKJ commented on Aug 30, 2021 •
using export $(grep -v '^#' .env | xargs)
could not export the following,
A=10
B=$A
C=${A}
now, echo $B
produces $A
while it should print 10
using
export $(grep -v '^#' .env | xargs)
could not export the following,A=10 B=$A C=${A}
now,
echo $B
produces$A
while it should print10
@ShivKJ Of course, it will equal to export A=10 B=$A C=${A}
and doesn't work like shell script. Another example is /etc/environment
have same behavior and don't accept variable definition.
kolypto commented on Sep 12, 2021 •
If you're wondering how to load a .env
file with direnv
: direnv already supports that :)
dotenv "testing.env"
jhud commented on Sep 20, 2021
The Problem
The problem with
.env
files is that they're like bash, but not completely.
While in bash you'd sometimes put quotes around values:name='value ! with > special & characters'
in
.env
files there are no special characters, and quotes are not supported: they're part of the value. As a result, such a string has to be escaped when imported into bash.The Solution
This version withstands every special character in values:
set -a source <(cat development.env | sed -e '/^#/d;/^\s*$/d' -e "s/'/'\\\''/g" -e "s/=\(.*\)/='\1'/g") set +a
Explanation:
-a
means that every bash variable would become an environment variable/^#/d
removes comments (strings that start with#
)/^\s*$/d
removes empty strings, including whitespace"s/'/'\\\''/g"
replaces every single quote with'\''
, which is a trick sequence in bash to produce a quote :)"s/=\(.*\)/='\1'/g"
converts everya=b
intoa='b'
As a result, you are able to use special characters :)
To debug this code, replace
source
withcat
and you'll see what this command produces.
Thank you! I have some long and unusually formatted environment variables, and this was the only solution which didn't choke on them.
~/bin/envs
set -a
source .env
set +a
exec $@
$> envs go run .
export $(grep -v '^#' .env | xargs)
Thanks so much. I love it.
export $(grep -v '^#' .env | xargs)
Hats off man
Doesn't work for me because of JWT in the .env with lots of newlines.
Had to manually vim copy paste the contents instead.
this worked for me, from @rjchicago snippetset -o allexport; source .env; set +o allexport
Here is a modified version of this code that allows for variable expansion
if [ -f .env ]; then export $(echo $(cat .env | sed 's/#.*//g'| xargs) | envsubst) fi
worked like a charm. Thanks!
FWIW If you need to just run a command with environment from an env file, this could help:
env $(cat .env|xargs) CMD
provided there are no other lines except env definitions in form of
VAR=VALUE
. Don't remember where I found it, but does the trick for me.
You can actually get away without xargs
env $(cat .env) <command>
export $( grep -vE "^(#.*|\s*)$" .env )
J5Dev commented on Nov 8, 2021
The cleanest solution I found for this was using
allexport
andsource
like thisset -o allexport source .env set +o allexport
This was by far the best solution here for me, removed all the complexity around certain chars, spaces comments etc. Just needed a tweak on formatting to prevent others being tripped up, should be:
set -o allexport
source .env
set +o allexport
The above worked fine for me, but thought I'd share the solution I went with: https://stackoverflow.com/a/30969768/179329
set -o allexport; source .env; set +o allexport
I like this too.
source .env
works for me
wont work if you have
#
in your .env
Thanks for this.
Great work ! :)
-
I have .env with [VAR1=xyz, VAR2=233, NPM_TOKEN=123]
-
Seems that this example from @valmayaki :
if [ -f .env ]; then
export $(echo $(cat .env | sed 's/#.*//g'| xargs) | envsubst)
fi
# always response as one liner with all the variables
- I switched to use this without xargs:
export "$(grep -vE "^(#.*|\s*)$" .env)"
# as it's responding with single value ex:
echo $NPM_TOKEN # just print the single variable
thanks to all for great cooperation <3
The above worked fine for me, but thought I'd share the solution I went with: https://stackoverflow.com/a/30969768/179329
set -o allexport; source .env; set +o allexport
This works!
The cleanest solution I found for this was using
allexport
andsource
like thisset -o allexport source .env set +o allexport
This was by far the best solution here for me, removed all the complexity around certain chars, spaces comments etc. Just needed a tweak on formatting to prevent others being tripped up, should be:
set -o allexport source .env set +o allexport
Many thanks for this solution reference
That 's great, thanks
wffranco commented 14 days ago •
using
export $(grep -v '^#' .env | xargs)
could not export the following,A=10 B=$A C=${A}
now,
echo $B
produces$A
while it should print10
this read line by line, allowing to use previous set variables
while read -r LINE; do
if [[ $LINE == *'='* ]] && [[ $LINE != '#'* ]]; then
ENV_VAR="$(echo $LINE | envsubst)"
eval "declare $ENV_VAR"
fi
done < .env
@chengxuncc
usingexport $(grep -v '^#' .env | xargs)
could not export the following,A=10 B=$A C=${A}
now,
echo $B
produces$A
while it should print10
this read line by line, allowing to use previous set variables
while read -r LINE; do if [[ $LINE == *'='* ]] && [[ $LINE != '#'* ]]; then ENV_VAR="$(echo $LINE | envsubst)" eval "declare $ENV_VAR" fi done < .env
This solution is helpful!
Thanks a lot!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK