Introduction
Shell variables are key-value pairs used to store important configuration data for the shell. Given that a variable is just a pointer to a piece of data, it can contain a reference to any data type - a name, number, filename, or even another variable.
All the variables the user defines inside the bash shell are local by default. It means that the shell's child processes do not inherit the shell's variables. The user must export the variables to make them available to child processes.
This tutorial will show you how to export Bash variables in Linux using the export
command.
Prerequisites
- Access to the terminal/command line.
- The bash shell.
What Does the export command in Bash do?
Variables in Bash are created using the declare command or by simply typing the key-value pair in the shell. For example, to create a variable named test
, which has a string value of example
, type:
test="example"
To see the value of a variable, use the echo command:
echo $test
The value appears in the output:
However, the variable created in this way applies only to the current shell session. To test this, open a child shell by typing:
bash
Use echo
to check the variable.
echo $test
The output is empty because the variable test
has a value only in the parent shell. A variable needs to be exported to be used in child processes.
Exporting variables is also important for Bash scripting. The example below creates a script named test-script.sh
that displays the value of the test
variable.
1. Create the file using a text editor such as nano:
nano test-script.sh
2. Type the following into the file:
#!/bin/bash
echo $test
3. Save the file and exit.
4. Change the permissions of the file to allow it to be executed:
chmod u+x test-script.sh
5. Execute the script:
./test-script.sh
The script returns an empty output.
The output is empty because the script is executed in a child shell that is automatically opened upon executing the script.
How to Export Bash Variable
The syntax for the export
command is simple:
export [variable-name]
The example below confirms that the test
variable exists even after using bash
to start a new shell session.
The scripts now also have access to the variable. Execute the previously created script again.
./test-script.sh
It now correctly outputs the value of test
.
Note: Variables can be created and exported in the same line. To do this, use the following syntax:
export [variable-name]="[value]"
The variable created this way is automatically exported to child processes.
Exporting Functions
Use the export
command to export bash functions.
1. For example, create a bash function called echoVar
:
function echoVar
2. The function calls echo
to display the value of the test
variable:
{
echo "The value of the test variable is: "$test
}
3. The function can now be called by its name to display the value of the variable. To make the function available in child processes, type:
export -f echoVar
The function is now available in child shells.
Viewing All Exported Variables
When the export
command is issued with no arguments, it displays the list of all variables. To export all the listed variables to child processes, use the -p
option.
export -p
The two variables created in this article are at the bottom of the output.
To undo the effect of export -p
, use the -n
option.
export -n
The variables are again limited to the current shell session.
Conclusion
This tutorial showed you how to export shell variables in Linux using the bash export
command. If you deal with bash commands regularly, read how to write a bash script with examples and learn how to automate bash command execution.