Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
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.
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.
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.
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.
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.
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.
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:
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.
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.