1. Overview

Exporting variables from a file is a common task for loading specific configurations or credentials. By exporting variables, configurations can be inherited by child processes and subshells.

In this tutorial, we’ll explore different ways to export variables from a file in Bash.

2. Sample Task

Let’s suppose we have a configuration file named .env:

$ cat .env
a="one"        # comment
b="two"        # comment

c="three four" # comment
#d=true        # comment

The file contains three variables, a, b, and c that we wish to export. In addition, it contains a blank line and a fourth variable, d, on a line that is commented out. Notably, we see that comments can follow variable declarations on the same line.

Our goal is to export all the variables in the file which aren’t commented out.

Let’s explore different methods to accomplish this task.

3. Exporting Variables by Sourcing a File

One approach to exporting variables from a file involves sourcing the file to load the variables into the current environment. We can either enable a special shell option for exporting variables and then source the file, or we can first source the file and then use the export command over the variables to be exported.

3.1. Using set -a

Perhaps the simplest approach to exporting variables from a file is to first enable the allexport shell option via set -a and then source the file:

$ set -a
$ . .env
$ set +a

At this point, by sourcing the .env file, the a, b, and c variables become available in the current environment and are also exported due to the allexport option. Finally, we reset the allexport option using set +a.

We can also view the exported variables using the export command with the -p option:

$ export -p
...
declare -x a="one"
declare -x b="two"
declare -x c="three four"

We see that the a, b, and c variables are among those exported.

3.2. Using export With sed and cut

Another approach is to first source the configuration file and then use the export command over the list of variables. To extract the list of variables, we can use sed and cut:

$ sed -E '/^\s*#/d; /^\s*$/d' .env | cut -d '=' -f 1
a
b
c

First, we use sed to delete lines that begin with a hash symbol preceded by zero or more whitespace characters since such lines represent comments. Using sed, we also delete lines that contain only zero or more whitespace characters as we consider these as blank lines. The -E option used with sed enables extended regex.

Then, we pipe the result from sed to the cut command to extract the variable names that precede an equal sign. We use the -d option with cut to set the delimiter to an equal sign and the -f option to specify the field number we wish to extract.

Therefore, we can now source the .env file and export the variables:

$ . .env && export $(sed -E '/^\s*#/d; /^\s*$/d' .env | cut -d '=' -f 1)

Notably, we use sed and cut within a command substitution to list the variable names. We also use the && logical operator to run the export command over the list of variables once we’ve successfully sourced the .env file.

3.3. Using export With grep and cut

Alternatively, we can use grep instead of sed:

$ grep -Ev '(^\s*#|^\s*$)' .env | cut -d '=' -f 1
a
b
c

We use the -E option with grep to enable extended regex and the -v option to exclude the specified pattern. Similar to the sed case, the pattern we exclude consists of either a hash symbol preceded by zero or more whitespace characters, or blank lines containing zero or more whitespace characters.

The solution is then similar in construct to the one used with sed:

$ . .env && export $(grep -Ev '(^\s*#|^\s*$)' .env | cut -d '=' -f 1)

In summary, we first source the .env file, and then we export the variables after extracting their names via grep and cut.

4. Exporting Variables Without Sourcing a File

Another approach to exporting variables from a file is by using the export command directly over the variable declarations, without sourcing the file:

$ cat export_variables.sh
#!/usr/bin/env bash
while read -r line; do
    line="$(echo "${line%%#*}" | xargs)"
    [ -n "$line" ] && export "$line"
done < .env

The export_variables.sh script performs several steps:

  1. use a while loop along with the read command to read the lines of the .env file, one by one, into a variable named line
  2. use the ${line%%#*} parameter expansion expression to remove hash symbols and all subsequent characters from each line
  3. echo the result of the parameter expansion and pipe the output to xargs to remove any leading or trailing whitespace characters
  4. use the test builtin with the -n option to check if the resulting line variable is non-empty, and if so, export it

Let’s grant the script execute permissions via chmod:

$ chmod +x export_variables.sh

Finally, let’s unset the variables and source the script:

$ unset a b c
$ . ./export_variables.sh

We can check the exported variables:

$ export -p
...
declare -x a="one"
declare -x b="two"
declare -x c="three four"

Consequently, we see that the three variables, a, b, and c were exported.

5. Conclusion

In this article, we explored different ways to export variables from a file in Bash.

One approach involved using set -a and then sourcing the file. Another approach required first sourcing the file and then exporting the variables via the export command. In that approach, we extracted the variable names using cut in conjunction with either sed or grep. Finally, a final method was to use a while loop to read and process the lines of the file and then export only valid lines that represent variable declarations.