Skip to content

Instantly share code, notes, and snippets.

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

abij commented Apr 19, 2021

@mopcweb Can you update with input and output, what is supported in the .env file?
And how much fun did you have, creating your own solution ;)?

# INPUT                     Expected:
'FOO=value'                 FOO='value'
"FOO=#value # comment"      FOO='#value # comment'
"FOO=value   "              FOO='value   '
'FOO='                      FOO=''
'export FOO=value'          export FOO='value'
"FOO=foo bar"               FOO='foo bar'
"FOO=   foo"                FOO='   foo'

Test cases from @ko1nksm:
Note: check his awesome script: !

Copy link

lzkill commented May 5, 2021

Why not dotenv-cli?

$ dotenv <command with arguments> 
# or
$ dotenv -e .env.custom <command with arguments>

Copy link

loopmode commented May 5, 2021

Because not every system has nodejs on it. And it's good that way.

Copy link

geoffjukes commented May 11, 2021

One-liner that allows unquoted variables that contain spaces:

OLD_IFS=$IFS; IFS=$'\n'; for x in `grep -v '^#.*' .env`; do export $x; done; IFS=$OLD_IFS

Copy link

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

MY_VAR=$(read_var MY_VAR .env)

Perfect, thanks

Copy link

What about this

# save the existing environment variables

# if the .env file exists, source it
[ -f .env ] && . .env

# re-export all vars from the env so they override what ever was set in .env
for e in $prevEnv
    export $e

Copy link

wieczorek1990 commented Jan 21, 2022

I wrote my own because was using forbidden symbols in envs.

This basically adds apostrophes so that all variables will be treated as strings. This way you can use your Docker env files and source them with source

import sys

def main(input_path, postfix='.sh'):
    with open(input_path, 'r') as file_handle:
        lines = file_handle.readlines()
        envs = {}
        for line in lines:
                parts = line.split('=')
                name = parts[0]
                value = ''.join(parts[1:]).rstrip('\n')
            except ValueError:
                envs[name] = value

    output_path = f'{input_path}{postfix}'
    with open(output_path, 'w') as file_handle:
        lines = []
        for name, value in envs.items():
            line = f'{name}=\'{value}\'\n'

if __name__ == '__main__':
    # Passes first argument as input path.

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