Last active
April 22, 2025 02:18
-
Star
(501)
You must be signed in to star a gist -
Fork
(59)
You must be signed in to fork a gist
-
-
Save mihow/9c7f559807069a03e302605691f85572 to your computer and use it in GitHub Desktop.
Load environment variables from dotenv / .env file in Bash
This file contains hidden or 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
# The initial version | |
if [ ! -f .env ] | |
then | |
export $(cat .env | xargs) | |
fi | |
# My favorite from the comments. Thanks @richarddewit & others! | |
set -a && source .env && set +a |
Also - recommend to use [[:space:]] rather than \s or [ \t] --- on Macs, \s isn't space, and \t isn't TAB but t. Yay standardization!
This is what I use:
# shellcheck disable=SC2046
[ -f .env ] && export $(grep -v '^#' .env | xargs)
No need to do a double negation with [ ! -f .env ] ||
when you can do [ -f .env ] &&
@bfontaine thanks, worked like a charm.
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 echo "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" }
working for my case: Windows + WSL + bash + 7z password from env + export from .env
Thanks @MansourM !
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" }Looks great, but it doesn't work if the .env file contains only 1 row (just one without br)
You can adjust the while loop like this:
read_env() {
local filePath="${1:-.env}"
if [ ! -f "$filePath" ]; then
echo "missing ${filePath}"
exit 1
fi
echo "Reading $filePath"
- while IFS= read -r LINE; do
+ while IFS= read -r LINE || [ -n "$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"
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I needed this and after reading the above realized none of the solutions quite worked for my .env file on a Mac... so I modified what @shadiabuhilal into this (which I put into a file called "readenv")