Skip to content

Instantly share code, notes, and snippets.

@andreibosco
Created May 6, 2021 15:08
Show Gist options
  • Save andreibosco/884048f8fab613bc5d3fb22cf7316911 to your computer and use it in GitHub Desktop.
Save andreibosco/884048f8fab613bc5d3fb22cf7316911 to your computer and use it in GitHub Desktop.
Set environment variables from file of key/value pairs

Source: https://stackoverflow.com/a/45971167/481690

set -a
. ./env.txt
set +a

If env.txt is like:

VAR1=1
VAR2=2
VAR3=3

Explanations -a is equivalent to allexport. In other words, every variable assignment in the shell is exported into the environment (to be used by multiple child processes). More information can be found in the Set builtin documentation:

-a Each variable or function that is created or modified is given the export attribute and marked for export to the environment of subsequent commands.

Using ‘+’ rather than ‘-’ causes these options to be turned off. The options can also be used upon invocation of the shell. The current set of options may be found in $-.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment