The export Command in Bash

Fumbani Banda Mar 22, 2022
  1. Bash export Command
  2. Using the export Command in Bash
  3. View All Exported Variables
The export Command in Bash

This tutorial explains the export command and its use on the bash shell.

Bash export Command

The export command is a built-in bash command used to make variables available to child processes of the current shell. Once you export a variable in a shell, any process executed from that shell will be able to access that variable.

The export command uses the following syntax.

export variable=value

The above command means that the processes started within the shell will access the variable.

Declaring a variable without the export command means the variable will only be available to the shell and not other processes within the shell.

variable=value

Limiting variables to the shell is used for loop variables and temporary variables.

Using the export Command in Bash

The image below defines a variable called name and assign it the value, Doe. Next, we use the echo command to display the name variable’s value and print the Doe to the standard output.

We start a bash subprocess inside the current shell, and we use the echo command to print out the value of the name variable. The echo command does not print the value of the name variable, only a blank space.

The echo command does not print the value of the name variable because we did not export the name variable using the export command. Subprocesses cannot access variables from the main process unless we export the variables.

bash export command - 1

Here, we use the export command, and we define a variable, name, and assign it the value Doe. We use the echo command to display the name variable’s value and print Doe to the standard output.

We create a bash sub-process inside the current bash shell and use the echo command to display the value of the name variable. The echo command displays the value of the name variable to the standard output.

The bash subprocess inside the shell process can access the name variable because of the export command. As the image below demonstrates, exporting a variable makes it accessible to the subprocesses inside the current shell.

bash export command - 2

View All Exported Variables

To view all the variables that have been exported in the current shell, use the following command.

export -p

The -p option displays a list of all the exported variables and functions in the current shell.

The image below shows part of the standard output displayed by the export command to show a list of all the exported variables in the current shell.

bash export command - 3

Fumbani Banda avatar Fumbani Banda avatar

Fumbani is a tech enthusiast. He enjoys writing on Linux and Python as well as contributing to open-source projects.

LinkedIn GitHub