Export Command in Linux Explained

The export command in Linux is used for creating environment variables. You can use it like this:

export myvar

or a shorthand like this to assign it a value immediately:

export myvar=5

You can see the value of exported variables with the echo command:

echo $myvar

To make the changes permanent, you should add it to the ~/.bashrc file.

That was just the quick summary. Let’s see it in details to understand it better.

Understanding how export command works

In the example below, I declare a shell variable var and assign it a value 3. It’s a shell variable for now.

abhishek@nuc:~$ var=3
abhishek@nuc:~$ echo $var
3

If I exit the terminal and open a new terminal, this shell variable will disappear. If I want to use this variable in a shell script, it won’t work. Similarly, if I switch user (and thus initiating a new shell with this user), this shell variable won’t be available:

abhishek@nuc:~$ su prakash
Password: 
prakash@nuc:/home/abhishek$ echo $var

Now, let’s go back to the previous user (and thus the previous shell where I declared the shell variable). You can see that the variable still exists here (because we didn’t terminate this shell session yet):

prakash@nuc:/home/abhishek$ exit
exit
abhishek@nuc:~$ echo $var
3

So, now if I use the export command on the variable var here, it will become an environment variable and it will be available to all the subshells, users and shell scripts in this session.

abhishek@nuc:~$ export var
abhishek@nuc:~$ echo $var
3
abhishek@nuc:~$ su prakash
Password: 
prakash@nuc:/home/abhishek$ echo $var
3

You can check all the environment variables using the printenv command:

printenv

Make exported shell variables ‘permanent’ with bashrc file

But the struggle doesn’t end here. If you close the session, exit the terminal, log out or reboot your system, your environment variable will disappear again.

This is why it’s a common practice to add the export commands to the runtime configuration (rc) file of your shell.

Every shell has this rc file located in the user’s home directory which is used to determine variables and other configuration when the shell is started. As a user, you can use this rc file to customize your shell and its behavior.

If you are using bash shell, you should have a bashrc file at ~/.bashrc. You can either edit this file in a text editor like Vim or you can just append export var=3 (or whatever you are exporting) to this file.

Once done, you should use the source command to make the changes available immediately.

A good practice is to keep all the user defined environment variables at one place.

Why use export command?

One of the most common use of the export command is when you want to add something to the path so that your Linux system will find the certain command/executable file.

For example, if you installed maven and you want to be able to run it, you should add the directory location of maven executables to the path like this:

export PATH=/opt/maven/bin:$PATH

What does it do? It adds this directory location to the path. When you try to run a command in Linux, your system searches for its executable (usually in bin directory) in the directories mentioned in the PATH variable.

abhishek@nuc:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
abhishek@nuc:~$ export PATH=/opt/maven/bin:$PATH
abhishek@nuc:~$ echo $PATH
/opt/maven/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

I highly recommend reading about Linux directory structure to have a better idea.

Bonus Tip: Remove a variable from exported list

Suppose you want to remove an ‘exported’ variable. You can use the negate option in this fashion:

export -n myvar

Keep in mind that this will not reset the value of the variable. It will just turn the exported global variable into a local variable. It will continue to have the same value you had set earlier.

If you want to remove the variable from the exported list as well as remove its assigned value, use the unset option:

unset myvar

I hope you have a better idea of the export command in Linux now. If you have doubts, please feel free to ask in the comment section.