Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Load environment variables from dotenv / .env file in Bash
if [ ! -f .env ]
then
export $(cat .env | xargs)
fi
@bergkvist
Copy link

bergkvist commented Jul 26, 2022

You can also do:

eval "$(
  cat .env | awk '!/^\s*#/' | awk '!/^\s*$/' | while IFS='' read -r line; do
    key=$(echo "$line" | cut -d '=' -f 1)
    value=$(echo "$line" | cut -d '=' -f 2-)
    echo "export $key=\"$value\""
  done
)"

This ignores empty lines, and lines starting with # (comments). If you replace eval with echo - you can inspect the generated code.

@richarddewit
Copy link

richarddewit commented Jul 28, 2022

The cleanest solution I found for this was using allexport and source like this

set -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

From man set:

       -o option
             This  option  is  supported if the system supports the User Portability Utilities op‐
             tion. It shall set various options, many of which shall be equivalent to  the  single
             option letters. The following values of option shall be supported:

             allexport Equivalent to -a.

So this is the same as

set -a            
source .env
set +a

@jairajsahgal
Copy link

jairajsahgal commented Jul 29, 2022

[ ! -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)

This one works for django .env

@SrEnrique
Copy link

SrEnrique commented Jul 31, 2022

this works for me

#!/usr/bin/env bash
. .env

@drjasonharrison
Copy link

drjasonharrison commented Sep 1, 2022

For those using sed to rewrite their .env files before evaluation by bash, for example the solution suggested by @kolypto in https://gist.github.com/mihow/9c7f559807069a03e302605691f85572?permalink_comment_id=3625310#gistcomment-3625310

I ran into another case that hadn't been considered: Windows line endings "\r\n". I'm now using:

    set -o allexport # enable all variable definitions to be exported
    source <(sed -e "s/\r//" -e '/^#/d;/^\s*$/d' -e "s/'/'\\\''/g" -e "s/=\(.*\)/=\"\1\"/g" "${ENV_FILE}")
    set +o allexport

@bolorundurovj
Copy link

bolorundurovj commented Sep 12, 2022

The cleanest solution I found for this was using allexport and source like this

set -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

From man set:

       -o option
             This  option  is  supported if the system supports the User Portability Utilities op‐
             tion. It shall set various options, many of which shall be equivalent to  the  single
             option letters. The following values of option shall be supported:

             allexport Equivalent to -a.

So this is the same as

set -a            
source .env
set +a

Worked for me

@rjchicago
Copy link

rjchicago commented Sep 12, 2022

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

As @richarddewit pointed out above, -a/+a can be used in place of -o allexport to be more concise (thanks!).

I now use the following simple line to source .env files into my scripts...

set -a; source .env; set +a

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