Parse a .env (dotenv) file directly using BASH
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
Why not dotenv-cli?
$ dotenv <command with arguments>
# or
$ dotenv -e .env.custom <command with arguments>
Because not every system has nodejs on it. And it's good that way.
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
ead_var() {
VAR=$(grep $1 $2 | xargs)
IFS="=" read -ra VAR <<< "$VAR"
echo ${VAR[1]}
}MY_VAR=$(read_var MY_VAR .env)
Perfect, thanks
What about this
# save the existing environment variables
prevEnv=$(env)
# 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
do
export $e
done
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 envs.sh
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:
try:
parts = line.split('=')
name = parts[0]
value = ''.join(parts[1:]).rstrip('\n')
except ValueError:
pass
else:
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'
lines.append(line)
file_handle.writelines(lines)
if __name__ == '__main__':
# Passes first argument as input path.
main(sys.argv[1])
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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 ;)?
Test cases from @ko1nksm: https://github.com/ko1nksm/shdotenv/blob/main/spec/docker_spec.sh.
Note: check his awesome script: https://github.com/ko1nksm/shdotenv !