Reputation: 26538
Let's say I have .env file contains lines like below:
USERNAME=ABC
PASSWORD=PASS
Unlike the normal ones have export
prefix so I cannot source the file directly.
What's the easiest way to create a shell script that loads content from .env file and set them as environment variables?
Upvotes: 111
Views: 157294
Reputation: 1
I'm using the Windows machine to run the WSL. Here is what I do to set environment variables from .env file that is created by Windows and made that an alias in ~/.zshrc
# ~/.zshrc
# Load set environment variables from .env file
load_env() {
if [ -f .env ]; then
dos2unix .env
export $(grep -v '^#' .env | xargs)
echo "Set environment variables from .env file"
else
echo ".env file not found in the current directory"
fi
}
alias le='load_env'
You should install the dos2unix to converting file .env to Unix format:
sudo apt-get install dos2unix
Hope this will help!
Upvotes: 0
Reputation: 907
You can use an external command line tool which loads the environment variables from the desired .env.*
file and runs the command of your choice directly, e.g. dotenvx or python-dotenv:
$ npm install @dotenvx/dotenvx --save
$ dotenvx run -f .env.example -- ./script.sh
$ pip install python-dotenv
$ dotenv -f .env.example run -- ./script.sh
Upvotes: 0
Reputation: 5993
To complete the excellent accepted answer: https://stackoverflow.com/a/43267603/1169705
Also catch for blank lines, other bash
will complain.
while IFS== read -r key value; do
if [[ ! -z "$key" ]]; then
printf -v "$key" %s "$value" && export "$key"
fi
done <.env
Upvotes: 0
Reputation: 351
This script works perfectly for me (Jun 2023).
#!/bin/sh
# Load environment variables from .env file
[ ! -f .env ] || export $(grep -v '^#' .env | xargs)
Example of .env file:
# Database settings
DB_HOST=localhost
DB_PORT=5432
DB_NAME=mydatabase
DB_USER=myuser
DB_PASSWORD=mypassword
# API keys
API_KEY=abc123
SECRET_KEY=def456
# Other settings
DEBUG_MODE=true
LOG_LEVEL=info
Upvotes: 13
Reputation: 35344
This is what I use:
load_dotenv(){
# https://stackoverflow.com/a/66118031/134904
# Note: you might need to replace "\s" with "[[:space:]]"
source <("$1" | sed -e '/^#/d;/^\s*$/d' -e "s/'/'\\\''/g" -e "s/=\(.*\)/='\1'/g")
}
set -a
[ -f "test.env" ] && load_dotenv "test.env"
set +a
If you're using direnv
, know that it already supports .env
files out of the box :)
Add this to your .envrc
:
[ -f "test.env" ] && dotenv "test.env"
Docs for direnv's stdlib: https://direnv.net/man/direnv-stdlib.1.html
Upvotes: 6
Reputation: 295363
export
commandThis requires appropriate shell quoting. It's thus appropriate if you would have a line like foo='bar baz'
, but not if that same line would be written foo=bar baz
set -a # automatically export all variables
source .env
set +a
The below reads key/value pairs, and does not expect or honor shell quoting.
while IFS== read -r key value; do
printf -v "$key" %s "$value" && export "$key"
done <.env
Upvotes: 209
Reputation: 3268
This will export everything in .env:
export $(xargs <.env)
Edit: this requires the environment values to not have whitespace. If this does not match your use case you can use the solution provided by Charles
Edit2: I recommend adding a function to your profile for this in any case so that you don't have to remember the details of set -a
or how xargs
works.
Upvotes: 74
Reputation: 26538
Found this:
while read line; do export $line; done < <(cat input)
UPDATE So I've got it working as below:
#!/bin/sh
while read line; do export $line; done < .env
Upvotes: 2