How to use the linux export command

The Linux export command makes a shell variable available to the programs you run from that shell. Without it, a variable stays local to your current session; with it, child processes and scripts can read the value too. In this article, we will show you the basic usage of the Linux export command with examples.

What the export command does

By default, a variable you set in the shell is a local variable: only the current shell can see it. The Linux export command promotes it to an environment variable, which every program and script started from that shell inherits. This matters when a tool reads its configuration from the environment, such as a database password or an application setting.

Export a variable

First, you can create and export a variable in one step:

export MY_VAR="hello"

Or export a variable you have already set:

MY_VAR="hello"
export MY_VAR

Add a folder to your PATH

Next, a common use is extending your PATH so the shell can find your own programs. Keep the existing value and append the new folder:

export PATH="$PATH:$HOME/bin"

Export a function

Additionally, you can export a function so it is available to child shells with the -f flag:

my_func() {
  echo "In my_func"
}
export -f my_func

List exported variables

Finally, run export -p to list every variable currently marked for export in your session:

export -p

Remember that an export only lasts for the current session. To make a variable permanent, add the export line to your shell profile, such as ~/.bashrc or ~/.bash_profile, so it is set every time you log in. To remove a variable from the environment, use unset MY_VAR.

Need more help?

Explore more Linux commands with examples on an SSH shared hosting account, or get full root control with a JetHost VPS.