Skip to content

Instantly share code, notes, and snippets.

@mihow
Last active April 12, 2024 15:33
Show Gist options
  • Save mihow/9c7f559807069a03e302605691f85572 to your computer and use it in GitHub Desktop.
Save mihow/9c7f559807069a03e302605691f85572 to your computer and use it in GitHub Desktop.
Load environment variables from dotenv / .env file in Bash
if [ ! -f .env ]
then
export $(cat .env | xargs)
fi
@kolypto
Copy link

kolypto commented Feb 9, 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 every a=b into a='b'

As a result, you are able to use special characters :)

To debug this code, replace source with cat and you'll see what this command produces.

@drjasonharrison
Copy link

@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

@mverteuil
Copy link

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

@kolypto
Copy link

kolypto commented Feb 13, 2021

@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 )

@IlanVivanco
Copy link

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

set -o allexport
source .env set
+o allexport

@asheroto
Copy link

asheroto commented Mar 28, 2021

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.

@ykshatroff
Copy link

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.

@asheroto
Copy link

asheroto commented Apr 1, 2021

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.

@asolera
Copy link

asolera commented Apr 9, 2021

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)

@asheroto
Copy link

asheroto commented Apr 9, 2021

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.

@ko1nksm
Copy link

ko1nksm commented Apr 12, 2021

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.

@asheroto
Copy link

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.

This is great!

@mihow
Copy link
Author

mihow commented Apr 13, 2021

Wow @ko1nksm! This looks incredibly thorough. I look forward to using it, thank you.

@GusGA
Copy link

GusGA commented Apr 17, 2021

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

@lionelkouame
Copy link

Thank you so much for your solution .

@muthugit
Copy link

muthugit commented May 4, 2021

source .env

works for me

@abhidp
Copy link

abhidp commented May 28, 2021

source .env

works for me

wont work if you have # in your .env

@muthugit
Copy link

@abhidp sorry, i didn't check that case...

@Bioblaze
Copy link

Bioblaze commented 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

@Bashorun97
Copy link

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

@quilicicf
Copy link

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

@maxweber3
Copy link

darbe habercisi

@msmans
Copy link

msmans commented Jul 31, 2021

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

@shlomi-viz
Copy link

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
Copy link

chengxuncc commented 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
Copy link

ShivKJ commented Aug 30, 2021

@chengxuncc

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

@chengxuncc
Copy link

@chengxuncc

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

@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
Copy link

kolypto commented Sep 12, 2021

If you're wondering how to load a .env file with direnv: direnv already supports that :)

dotenv "testing.env"

Docs: https://direnv.net/man/direnv-stdlib.1.html

@jhud
Copy link

jhud commented 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 every a=b into a='b'

As a result, you are able to use special characters :)

To debug this code, replace source with cat 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.

@jquick
Copy link

jquick commented Sep 21, 2021

~/bin/envs

set -a
source .env
set +a
exec $@

$> envs go run .

@lsotoj
Copy link

lsotoj commented Sep 21, 2021

export $(grep -v '^#' .env | xargs)

Thanks so much. I love it.

@aslamanver
Copy link

export $(grep -v '^#' .env | xargs)

Hats off man

@xmarkclx
Copy link

xmarkclx commented Oct 7, 2021

Doesn't work for me because of JWT in the .env with lots of newlines.
Had to manually vim copy paste the contents instead.

@nickbien
Copy link

this worked for me, from @rjchicago snippet
set -o allexport; source .env; set +o allexport

@geekwhocodes
Copy link

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!

@NatoBoram
Copy link

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>

@fzancan-SpazioCodice
Copy link

export $( grep -vE "^(#.*|\s*)$" .env )

@J5Dev
Copy link

J5Dev commented Nov 8, 2021

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

@MrYutz
Copy link

MrYutz commented Nov 11, 2021

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.

@n1k0
Copy link

n1k0 commented Mar 1, 2022

oh-my-zsh users can also activate the dotenv plugin.

@BinitaBharati
Copy link

source .env

works for me

wont work if you have # in your .env

Thanks for this.

@xswirelab
Copy link

@devzom
Copy link

devzom commented Mar 16, 2022

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

@michchan
Copy link

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!

@alphanetEX
Copy link

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

Many thanks for this solution reference

@theofanisv
Copy link

That 's great, thanks

@wffranco
Copy link

wffranco commented Jun 29, 2022

@chengxuncc

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

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

@Krong1997
Copy link

@wffranco

@chengxuncc
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

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!

@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

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

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

this works for me

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

@drjasonharrison
Copy link

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

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

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

@miedza
Copy link

miedza commented Sep 29, 2022

export $(awk -F= '{output=output" "$1"="$2} END {print output}' aaa.env)

@bergpb
Copy link

bergpb commented Dec 9, 2022

[ ! -f .env ] || export $(grep -v '^#' .env | xargs)

Sweet, works like a charm for me, thanks.

@bruteforks
Copy link

oh-my-zsh users can also activate the dotenv plugin.

thank you this was better

@C-Duv
Copy link

C-Duv commented Jan 20, 2023

I had troubles with a (Docker) setup where environment variables had spaces in their value without quotes and I needed to get the container's env. vars. in a script called during the container execution/runtime.

I ended getting the variables in the entrypoint, exporting them to a file and them reading them when needed.

# In entrypoint
export -pn \
    | grep "=" \
    | grep -v -e PATH -e PWD -e OLDPWD \
    | cut -d ' ' -f 3- \
    > /docker-container.env

The export command fixes issues with missing quotes, avoiding errors where the shell interpreter tries to execute parts of the variable value as commands.

# In script
set -o allexport
. /docker-container.env
set +o allexport

(I had to use /bin/sh so not using source file but . file)

@usmanhalalit
Copy link

oh-my-zsh users can also activate the dotenv plugin.

Fantastic! Thanks @n1k0!

@spazm
Copy link

spazm commented Mar 1, 2023

Posix compliant version built around set, [ ] and . Many thanks to the prior posters who brought up set -o a and set -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.

if [ -z "${-%%*a*}" ]; then
    set -a
    . ./.env
    set +a
else
    . ./.env
fi

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 like set -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 $- contains a 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.

if [ "$-" = "${-%a*}" ]; then
    # allexport is not set
    set -a
    . ./.env
    set +a
else
    . ./.env
fi

@guillermodlpa
Copy link

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
)"

@khoahuynhdev
Copy link

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

@simonrouse9461
Copy link

A simple solution that works for bash, zsh, and fish:

eval export $(cat .env)

@lalten
Copy link

lalten commented Jul 11, 2023

Use this to create the file

export -p > .env

and just

. .env

to read it back in

From man export:

The shell shall format the output, including the proper use of quoting, so that it is suitable for reinput to the shell as commands that achieve the same exporting results

@ddosia
Copy link

ddosia commented Aug 2, 2023

Although set -a; source .env; set +a is elegant and short, one feature which I missed is this overwrite existing exported variables.
I my use case I have a script, which connects to postgres with a predefined user. This user is stored in .env file as PG_USER=myuser. So the script does the magical set -a; source .env; set +a and everything works. But sometimes I need ad-hoc change the user. So what I'd do is PG_USER=postgres ./my_script.sh. In order not to over write the existing var I did this horrendous piece of code:

IFS=$'\n'
for l in $(cat /etc/my_service/.env); do
    IFS='=' read -ra VARVAL <<< "$l"
    # If variable with such name already exists, preserves it's value
    eval "export ${VARVAL[0]}=\${${VARVAL[0]}:-${VARVAL[1]}}"
done
unset IFS

@dangvanduc90
Copy link

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

work like a charm. ty

@MansourM
Copy link

MansourM commented Nov 24, 2023

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 was working the best for me but this still has 2 problems

  1. code breaks if the value has () characters inside it
  2. can not be used inside a function

here is my solution:

read_env() {
  local filename="${1:-.env}"

  if [ ! -f "$filename" ]; then
    echo "missing ${filename} file"
    exit 1
  fi

  echo "reading .env file..."
  while read -r LINE; do
    if [[ $LINE != '#'* ]] && [[ $LINE == *'='* ]]; then
      export "$LINE"
    fi
  done < "$filename"
}

UPDATED VERSION BELOW

@emilwojcik93
Copy link

emilwojcik93 commented Jan 7, 2024

Hi, here is mine solution to read vars from /etc/environemnt file which I used in /etc/profile or /etc/bash.bashrc

oneliner for easier validation if line exists in file
(I removed single quotes ' from conditions, so it could be easier parsed by grep)

while read -r LINE; do [[ ${LINE} =~ ^# || ${LINE} =~ ^PATH= || ! ${LINE} == *=* || ${LINE} =~ ^[0-9] || ${LINE} =~ ^[^a-zA-Z_] ]] || export "${LINE}"; done < "/etc/environment"

or formatad syntax:

while read -r LINE; do 
  if [[ ${LINE} =~ ^# || ${LINE} =~ ^PATH= || ! ${LINE} == *=* || ${LINE} =~ ^[0-9] || ${LINE} =~ ^[^a-zA-Z_] ]]; then
    continue
  else
    export "${LINE}"
  fi
done < "/etc/environment"

@shadiabuhilal
Copy link

shadiabuhilal commented Feb 2, 2024

Hi,

Here is my solution to read vars from .env file and ignoring # and cleaning values from ' and ".

https://gist.github.com/shadiabuhilal/220aa09f9bb83caed93a1f87401fcc60

dot-env.sh File:

#!/bin/bash

# Specify the path to your .env file
ENV_FILE=".env"

# Check if the .env file exists
if [ -f "$ENV_FILE" ]; then

  echo "[INFO]: Reading $ENV_FILE file."

  # Read the .env file line by line
  while IFS= read -r line; do
    # Skip comments and empty lines
    if [[ "$line" =~ ^\s*#.*$ || -z "$line" ]]; then
      continue
    fi

    # Split the line into key and value
    key=$(echo "$line" | cut -d '=' -f 1)
    value=$(echo "$line" | cut -d '=' -f 2-)

    # Remove single quotes, double quotes, and leading/trailing spaces from the value
    value=$(echo "$value" | sed -e "s/^'//" -e "s/'$//" -e 's/^"//' -e 's/"$//' -e 's/^[ \t]*//;s/[ \t]*$//')

    # Export the key and value as environment variables
    export "$key=$value"
  done < "$ENV_FILE"
  echo "[DONE]: Reading $ENV_FILE file."
else
  echo "[ERROR]: $ENV_FILE not found."
fi

Enjoy :)

@cihadturhan
Copy link

Thanks!
One-liner while running command a script (such as pnpm script)
I added parentheses not to pollute global environment vars. Not sure if it's needed though.

(export $(cat .env | xargs) && pnpm compile)

@rrakso
Copy link

rrakso commented Feb 28, 2024

source .env

works for me

wont work if you have # in your .env

@abhidp it seems, that it works well - even with comments in .env! :D (cc: @muthugit)

@MansourM
Copy link

this is the final version that im using, seems to work for all situations

read_env() {
  local filePath="${1:-.env}"

  if [ ! -f "$filePath" ]; then
    echo "missing ${filePath}"
    exit 1
  fi

  log "Reading $filePath"
  while read -r LINE; do
    # Remove leading and trailing whitespaces, and carriage return
    CLEANED_LINE=$(echo "$LINE" | awk '{$1=$1};1' | tr -d '\r')

    if [[ $CLEANED_LINE != '#'* ]] && [[ $CLEANED_LINE == *'='* ]]; then
      export "$CLEANED_LINE"
    fi
  done < "$filePath"
}

@benoit-cty
Copy link

Thanks @MansourM !

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