The Linux export command is built into the bash shell and has one main purpose – marking environment variables that should be passed to child processes. Critically, it does so without affecting other environments.

Why is this needed? Well, every time you open a new shell session, environment variables are set. If you need to change any of these variables during your session, then the shell itself has no way to select the change. This is where the Linux export command comes in – you can update the current session with changes you have made to the child process.

This is perhaps best explained by some examples:

Example 1:  Changing LANG with the Linux export command

One example of an environment variable is LANG in Ubuntu, which determines the language in which programs will communicate.

One way to find your current LANG value is by executing the export command using the -p option, like so:

export -p LANG

Bash will output a list of all exported variables on your system and their values, including LANG. This is useful if you don't know exactly what variable name you're looking for. Here's a snippet:

declare -x DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/0/bus"
declare -x HOME="/root"
declare -x LANG="C.UTF-8"
declare -x LESSCLOSE="/usr/bin/lesspipe %s %s"
declare -x LESSOPEN="| /usr/bin/lesspipe %s"
declare -x LOGNAME="root"
declare -x USER="root"
declare -x XDG_DATA_DIRS="/usr/local/share:/usr/share:/var/lib/snapd/desktop"
declare -x XDG_RUNTIME_DIR="/run/user/0"
declare -x XDG_SESSION_CLASS="user"
declare -x XDG_SESSION_ID="1"
declare -x XDG_SESSION_TYPE="tty"

An easier way to query find the current value of LANG, however, is to run echo $LANG. This will output your current language, which in our case is C.UTF-8.

Now let's install the German locale and use the Linux export command to change the LANG variable to it:

locale-gen de_DE.utf8
export LANG=de_DE.utf8

If we run echo $LANG again, we'll see that its value is now LANG=de_DE.utf8. Type vi test and you'll see that the language is now German.

Example 2: Exporting your own variable

You can export your own bash variables as well as modifying already exported ones. Let's first create a variable:


If we type echo $yourvar, you'll see that it returns an output of three.

But what if we move to a different shell instance, for example, by switching user? echo $yourvar doesn't return an output.

Now let's switch back to the original shell and make it an environment variable with the Linux export command:

export yourvar=3

Now it is available as an environment variable to all subshells, users, and shell scripts in the session:

echo $yourvar

su otheruser

echo $yourvar

Example 3:  Adding something to the path

This is potentially the most common use of the export command. Adding something to the path lets your system easily find the command or executable file. For example, you can add a bash script saved in the /bin directory:

export PATH=~/bin/$PATH

Now when you type anywhere, it will run the script – in our case, by printing "Hello World!".

Making your changes permanent

If you want your changes to be maintained after you exit the session, you need to save them to your ~/.bashrc file.

To do so, edit .bashrc with sudo nano ~/.bashrc or sudo vim ~/.bashrc and add the text you typed after your export command to the bottom of the file. For example: