Bash: Import variables from a .env file

1 minute read Published: 2021-07-26

This sets and exports all variables set in a .env file that can also be used by systemd to setup environment variables.

# Read and export the variables from .env
source "$HOME/.env"
while read -r var; do
        export "${var?}"
done < <(sed -e 's_=.*$__' "$HOME/.env")

Update: Apparently set -a / set +a before and after the sourcing makes it a lot easier. Thanks @bekopharm@social.tchncs.de for this hint.

#bash #systemd