Bash Export Variable

December 30, 2021

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.

How to export Bash variable

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:

Printing the value of a variable using the echo command.

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.

The variable set in the parent shell is not visible in the child shell.

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.

Running the test script without exporting the variable it should print.

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.

After the export command, the shell variable is visible in the child shell.

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.

Running the test script after the variable has been exported.

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
}
Creating a function in the Bash shell.

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.

Executing the function in a child process.

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
Exporting and displaying all the shell variables with the export -p command.

The two variables created in this article are at the bottom of the output.

Finding the two previously created variables in the output of the export -p command.

To undo the effect of export -p, use the -n option.

export -n

The variables are again limited to the current shell session.

Note: Environment variables are system configuration variables present in all major operating systems. Read the following articles to learn how to set them up in Windows, macOS, and Linux.

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.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
How to Set Environment Variables in Linux
December 5, 2024

Environment variables play a significant role in the process of setting up shell session environments. In this tutorial, you will learn how to set, view, and unset environment variables in Linux.
Read more
Bash Function & How to Use It
November 3, 2021

A bash function is a method used in shell scripts to group reusable code blocks. This article provides a complete overview of bash functions, illustrating how they work, and how to use them.
Read more
How To Run A Bash Script
December 9, 2021

Bash scripts store commands that often go together, such as updates and upgrades, to accomplish certain tasks automatically. Follow this tutorial to learn how to run a Bash script using various methods.
Read more
Linux set Command & How to Use it
November 23, 2021

On Unix-like operating systems, the set command functions within the Bourne shell (sh), C shell (csh), and Korn shell (ksh). In this tutorial, you will learn what the set command is and how to use it.
Read more