How to Use Export Command in Linux [Explained for Beginners]

The export command on Linux is used to create environment variables. You can use it

like this:export myvar

or an abbreviation like this to assign a

value to it immediately:export myvar=5

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

$myvar

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

.

That was just the quick summary. Let’s look at it in detail to understand it better.

Understand how the export

Export Command

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

[email protected]:~$ var=3 [email protected]:~$ 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 change

users (and therefore start a new shell with this user), this shell variable will not be available:[email protected]:~$ your prakash Password: [email protected]:/home/abhishek$ echo $var

Now, let’s go back to the previous user (and therefore to the previous shell where I declared the shell variable). You can see that the variable still exists here (because we haven’t finished

this shell session yet):[email protected]:/home/abhishek$ exit exit [email protected]:~$ echo $var 3

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

[email protected]:~$ export var [email protected]:~$ echo $var 3 [email protected]:~$ su prakash Password: [email protected]:/home/abhishek$ echo $var 3

You can check all environment variables using printenv:printenv command

Make exported shell variables ‘permanent’ with bashrc file

But the fight doesn’t end here. If you log out, exit the terminal, log out, or restart your system, your environment variable will disappear again.

This is why it is

common practice to add the export commands to your shell’s run-time (rc) configuration

file.

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

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

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

A good practice is to keep all user-defined environment variables in one place.

Why use the export command?

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

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

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

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

[email protected]:~$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin [email protected]:~$ export PATH=/opt/maven/bin:$PATH [email protected]:~$ echo $PATH /opt/maven/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/sbin:/sr/games:/usr/local/games:/snap/bin

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

Bonus tip: Remove a variable from the exported list

Suppose you want to delete an ‘exported’ variable. You can use the deny option like this:

export -n myvar

Note that this will not reset the value of the variable. It will simply convert the exported global variable to a local variable. It will still have the same value that you had previously set.

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

myvar option

I hope you have a better idea of the export command on Linux now. If in doubt, feel free to ask in the comments section.