Saturday, 1 June 2019

Use your .env file in bash


The .env project has take over the configuration variable war's since ruby started the project some time ago, It's getting some competition from ymal however I can't see me moving until I find a good bash solution.

The main power of .env is that there is an implementation for every language. Below is a snippet for using .env file in bash although I haven't tried it I can't see why it wouldn't work in sh of zsh

#
# First test if your .env file exsites
#
if [[ -f "$SRC_DIR/.env" ]]; then

    #
    # Mark all .env variable for export
    # See: https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
    #
    set -a;

    #
    # Souce our .env file
    #
    source  "$SRC_DIR/.env";

    #
    # Turn off exporting all variables
    #
    set +a

fi
#
# One liner
#
[[ -f "$SRC_DIR/.env" ]] && set -a; source  "$SRC_DIR/.env"; set +a