Created
March 26, 2019 13:46
-
-
Save RopoMen/21d28400a790e1b7007fd9851826be78 to your computer and use it in GitHub Desktop.
Bash script for .env file loading
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
#!/bin/bash | |
source logging_utils.sh | |
DOTENV_ENV="test" | |
DOTENV_BASE="./" | |
if [ ! -z $1 ]; then | |
DOTENV_ENV="$1" | |
fi | |
if [ ! -z $2 ]; then | |
DOTENV_BASE="$2" | |
fi | |
log_info "Loading dotenv files for environment \"$DOTENV_ENV\" with base path \"$DOTENV_BASE\"" | |
function load_dotenv_file() { | |
FILE="$1" | |
if [ -f $FILE ]; then | |
log_info "Loading dotenv file \"$FILE\"" | |
set -o allexport | |
source $FILE | |
set +o allexport | |
else | |
log_warn "Unable to load dotenv file \"$FILE\", file does not exist." | |
fi | |
} | |
# 1. always try to load ".env" | |
load_dotenv_file "$DOTENV_BASE.env" | |
# 2. then try to load ".env.DOTENV_ENV" | |
load_dotenv_file "$DOTENV_BASE.env.$DOTENV_ENV" | |
# 3. finally try to load ".env.local" | |
load_dotenv_file "$DOTENV_BASE.env.local" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
my_script.sh
Now script will try to load following .env files, each file can override variables defined in previously loaded file.