Instantly share code, notes, and snippets.

Embed
What would you like to do?
Parse a .env (dotenv) file directly using BASH
# Pass the env-vars to MYCOMMAND
eval $(egrep -v '^#' .env | xargs) MYCOMMAND
# … or ...
# Export the vars in .env into your shell:
export $(egrep -v '^#' .env | xargs)
@kowalk

This comment has been minimized.

Copy link

kowalk commented Feb 16, 2018

If you want just set a single variable in your script just use code below:
MY_VAR=$(grep MY_VAR .env | xargs)
IFS='=' read -ra MY_VAR <<< "$MY_VAR"
MY_VAR=${MY_VAR[1]}

@rafaelbeckel

This comment has been minimized.

Copy link

rafaelbeckel commented Mar 17, 2018

read_var() {
    VAR=$(grep $1 $2 | xargs)
    IFS="=" read -ra VAR <<< "$VAR"
    echo ${VAR[1]}
}

MY_VAR=$(read_var MY_VAR .env)
@desprit

This comment has been minimized.

Copy link

desprit commented Mar 26, 2018

MY_VAR=$(grep MY_VAR .env | xargs)
MY_VAR=${MY_VAR#*=}

dunno about possible drawbacks though

@pronebird

This comment has been minimized.

Copy link

pronebird commented May 27, 2018

It won't work if you have spaces, i.e FOO="BAR BUZ" will be returned without quotes from xargs

@revolter

This comment has been minimized.

Copy link

revolter commented May 30, 2018

Use xargs -d '\n' on GNU systems or xargs -0 on BSD systems to handle values with spaces.

https://stackoverflow.com/a/32589977/865175

@amberdiehl

This comment has been minimized.

Copy link

amberdiehl commented Aug 10, 2018

I have nothing technical to add; just saying thank you. This is exactly what I needed.

@dovidweisz

This comment has been minimized.

Copy link

dovidweisz commented Aug 30, 2018

Here's a one-liner for getting one variable:

MY_VAR=$(grep MY_VAR .env | cut -d '=' -f2)

One issue i see with all these solutions is: what happens if i have two similar variables. I need to fix the Grep part to only match ^MY_VAR=. I'm just not that good with grep.

Also... i would never have thought about using grep here, so Thank You.

@damcclean

This comment has been minimized.

Copy link

damcclean commented Nov 10, 2018

Perfect! Exactly what I was looking for :)

@twalling

This comment has been minimized.

Copy link

twalling commented Dec 14, 2018

I had to change the one-liner by @dovidweisz so that all of my var was included. If you have equal signs in your var which we're also using as the delimiter then you have to tell cut to include all the rest of the fields

MY_VAR=$(grep MY_VAR .env | cut -d '=' -f 2-)
@msmans

This comment has been minimized.

Copy link

msmans commented Dec 20, 2018

With GNU grep, you can simply do MY_VAR=$(grep -oP '^MY_VAR=\K.*' .env)

@Jules-Baratoux

This comment has been minimized.

Copy link

Jules-Baratoux commented Dec 26, 2018

I use the following to avoid overriding already set variable:

source <(grep -v '^#' .env | sed -E 's|^(.+)=(.*)$|: ${\1=\2}; export \1|g')
@heycarsten

This comment has been minimized.

Copy link

heycarsten commented Jan 2, 2019

I’ve been doing it like this:

# Load up .env
set -o allexport
[[ -f .env ]] && source .env
set +o allexport

It seems to work great… are their drawbacks to this?

In my case I’m just loading in the variables to use for in-project commands (in bin)

✌️

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