-
-
Save mihow/9c7f559807069a03e302605691f85572 to your computer and use it in GitHub Desktop.
# The initial version | |
if [ ! -f .env ] | |
then | |
export $(cat .env | xargs) | |
fi | |
# My favorite from the comments. Thanks @richarddewit & others! | |
set -a && source .env && set +a |
@kolypto this doesn't work on comments after the assignment:
c=3 # three is prime
or with assignments to previously defined variables:
q='this string'
s=$q
export $(echo $(cat .env | sed 's/#.*//g'| xargs) | envsubst)
This is the best one for me as I desperately needed POSIX variable substitution to work
@drjasonharrison the examples that you show are not a .env
file; it's a bash file :)
If you want comments, and assigmnents, in the file that you're going to load, you don't need this thread at all ))) Just source
it! bash can source bash )
The cleanest solution I found for this was using allexport
and source
like this
set -o allexport
source .env set
+o allexport
For anyone researching in search engines (as that's how I came across this), here's my take on it, combining @kolypto addition of substitution + fixes, original poster's use of confirming the .env
file exists, several mentioning the use of set -a
# Confirm .env file exists
if [ -f .env ]; then
# Create tmp clone
cat .env > .env.tmp;
# Subtitutions + fixes to .env.tmp2
cat .env.tmp | sed -e '/^#/d;/^\s*$/d' -e "s/'/'\\\''/g" -e "s/=\(.*\)/='\1'/g" > .env.tmp2
# Set the vars
set -a; source .env.tmp2; set +a
# Remove tmp files
rm .env.tmp .env.tmp2
fi
I'm favorable of cases with incorrect variable usage in .env
test=hi there
Using the above method will fix the issue automatically.
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.
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.
Well that could have saved me an hour or two. 😁 The main reason for my answer was to incorporate the fixes that @kolypto mentioned.
Thanks for the info.
I had some problems with carriage return (\r) with dotenv files created via VS Code, so i'd like to share the code i used that resolved it:
export $(echo $(cat .env | sed 's/#.*//g' | sed 's/\r//g' | xargs) | envsubst)
I had some problems with carriage return (\r) with dotenv files created via VS Code, so i'd like to share the code i used that resolved it:
export $(echo $(cat .env | sed 's/#.*//g' | sed 's/\r//g' | xargs) | envsubst)
Thank you, great addition!
Since .env is so widely used, I think it would be a great addition to native Linux. Any clue how we could submit this as an addition to different distros? I mean integrated into the OS itself. I'm familiar with Linux and its commands but not how to submit an idea. I suppose it would be something I could fork and submit a pull request for.
Seeing that this thread has been going on for long years, I figured we need a dotenv tool for the shell.
And I wrote it.
https://github.com/ko1nksm/shdotenv
There is no formal specification for .env, and each is slightly different, but shdotenv supports them and correctly parses comments, whitespace, quotes, etc. It is a single file shell script that requires only awk and runs lightly.
There is no need to waste time on trial and error anymore.
Seeing that this thread has been going on for long years, I figured we need a dotenv tool for the shell.
And I wrote it.
https://github.com/ko1nksm/shdotenvThere is no formal specification for .env, and each is slightly different, but shdotenv supports them and correctly parses comments, whitespace, quotes, etc. It is a single file shell script that requires only awk and runs lightly.
There is no need to waste time on trial and error anymore.
This is great!
Wow @ko1nksm! This looks incredibly thorough. I look forward to using it, thank you.
FWIW - I found this helpful, but it didn't do what I had expected. Modified it slightly to result in the following.
if [ -f .env ] then export $(cat .env | sed 's/#.*//g' | xargs) fi
👍 * 100
Thank you so much for your solution .
source .env
works for me
source .env
works for me
wont work if you have #
in your .env
@abhidp sorry, i didn't check that case...
# 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 ]
😢
[ ! -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)
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.
If you're wondering how to load a .env
file with direnv
: direnv already supports that :)
dotenv "testing.env"
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 +aExplanation:
-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 .
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:
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.