Skip to Content

Mastering Linux Export Command

The export command is a bash shell BUILTIN command. It tells the Linux shell to make the variables available to the child processes. In Linux, the export command is usually used to change local variables to environment variables.

We can use the export command to list all the global variables. It is the same as the env command in this case.

We can also assign a value to a variable and then make this variable available to the shell’s environment with the export command.

This means that the variable can be accessed by the sub-shell.

I will explain this with examples later.

What are variables in Linux?

The Bash shell uses a feature called variables to store information about the shell session.

There are two variable types in the Bash shell:

  • Local Variables − A local variable is a variable that is present within the current instance of the shell. It is not available to programs that are started by the shell. They are set at the command prompt.
  • Environment Variables − An environment variable is available to any child process of the shell. Some programs need environment variables in order to function correctly. Usually, a shell script defines only those environment variables that are needed by the programs that it runs.


Environment variables are visible from the shell session and from any spawned child sub shells. Local variables are available only in the shell that creates them.

The export command is used to make local variables to environment variables.

What is shell and sub shell in Linux?

In the Linux operating system, the shell is the software that provides a command-line interface (CLI) for interacting with the operating system.

When you open a terminal window and type a command, the shell processes that command and then displays the results.

We can use the following command to get process id of the current shell.

echo $$

A sub-shell is a new shell instance that is created and run within the context of parent shell.

Sub-shells can be useful in a number of situations, such as running a set of commands in a separate environment or creating a new environment for a script to run in.

How to create a sub shell in Linux?

We can run bash to create a sub shell.

$echo $$
$ ps -ef|grep 884397
ocp 884397 884169 0 07:58 pts/0 00:00:00 bash

So we can see that process 884397 is sub shell of process 884169.

List all environment variables with export command in Linux

To view a list of all environment variables that are set in your shell, you can use the export or env command.

$ export
declare -x DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/50291/bus"
declare -x HISTCONTROL="ignoredups"
declare -x HISTSIZE="1000"
declare -x HISTTIMEFORMAT="%F %T "
declare -x HOME="/home/ocp"
declare -x HOSTNAME=""
declare -x LANG="C.UTF-8"
declare -x LESSOPEN="||/usr/bin/ %s"
declare -x LOGNAME="ocp"

You can use echo command to get the value of a specific variable.

echo $HOME

Note:The set command displays environment variables and local variables, user-defined variables, and local functions. To unset an variable, you can use the unset command followed by the name of the variable. 

Set environment variables with export command in Linux

In the following example, a child shell was spawned via the bash command. The user-defined my_variable was not available in the child shell.

This is demonstrated by the blank line returned after the echo $my_variable command.

After the child shell was exited and returned to the original shell, the local variable was available.

$ my_variable="Hello World"
$ bash
$ echo $my_variable


$ exit
$ echo $my_variable
Hello World


Let’s export this local variable my_variable to environment variable. This is done by using the export command and the variable name (minus the dollar sign):

$ my_variable="I am Global now"
$ export my_variable
$ echo $my_variable
I am Global now
$ bash
$ echo $my_variable
I am Global now
$ exit
$ echo $my_variable
I am Global now

After defining and exporting the local variable my_variable , a child shell was started by the bash command. The child shell was able to properly display the my_variable variable’s value.

The variable kept its value, because the export command made it global.

To keep typing to a minimum, you can set the variable and export it all in one command. Using the previous example, you would type

export my_variable="I am Global Now"

and press Enter at the command line.

Environment variables are visible from any child processes created by the parent process that sets the variable. The method used to create a global environment variable is to first create a local variable and then export it to the global environment.


Let me summarize what we get.

Before using the export command, the variable does not exist in the sub shell’s environment. This means that programs run within the shell will not be able to access the variable.

After using the export command to set an global variable, the variable is added to the shell’s environment and its value is set to the value specified in the export command.

This means that programs run within the shell and sub shell can access the variable and can use its value to customize their behavior.

For example, consider the following export command:

export PATH=$PATH:/usr/local/bin

Before this command is run, the PATH variable only works in the current shell env. The shell does not know to search the /usr/local/bin directory for executables when you open a sub shell.

After this command is run, the PATH variable is set in the shell’s environment and its value includes the /usr/local/bin directory.  The sub shell will search the /usr/local/bin directory for executables.

Note that the changes made by the export command are only effective within the current shell.

If you open a new shell or log out and log back in, the environment variables will be reset to their default values. To permanently set an environment variable, you will need to add the export command to your shell’s startup script (e.g., ~/.bashrc for the Bash shell).