How to Set and Unset Environment Variables in Linux

"MY_VAR=value" and "unset MY_VAR" on a blue gradient background

In this tutorial, you will be acquainted with multiple methods to manage environment variables by checking all available variables (local and global), along with common ways to set/unset system-wide and remote login session accessible variables.

What Are Environment Variables

To understand environment variables, we have to start by defining what a variable is.

A variable is a character string to which we assign some value. The value assigned could be a number, text, filename, or any other known type of data. A variable is nothing more than a pointer to the actual data.

How about Environment? Every instance of a running shell has a number of variables. These variables are local to this specific shell. This type of variable is called the Local variable. Other variables are duplicated by the system from the current shell into all of its children.

This type of variable is called Environment variable (these variables are shared by the parent and child instances).

To clarify the child and parent shell instances, in Linux, when a program invokes another Linux program. We call the invoking program: the “parent,” and we call the invoked program the child. Likewise, the invoking shell instance is the parent, and the invoked shell instance is its child.

Therefore, an environment variable is a character string that stores a value that is used by an operating system and/or utilized by the processes of the currently logged-on user.

Check some examples of environment variables and their proposes:

HOME Contains the path name of your home directory. This is also the default directory for the cd command.
BASH_VERSION Contains the version of this instance of bash.
PS1 Defines the makeup and style of the command prompt used when the shell is interactive. The default value is $.
SHELL Holds the full path name of the current shell.
EDITOR The name of -or path to- your text editor. Its value is set by the user in the shell configuration file. Some programs check this variable value to launch the user’s favorite text editor.
PWD Holds the name of the working directory. When it has no value, the working directory name is assigned to PWD.
PS2 Defines the makeup and style of the secondary command prompt, or continuation prompt, its default value is >.

How to List Environment Variables

Linux offers us different types of ways to list, check, or search our system’s environment variables. In this section, we will take a look at the common tricks to printout environment and local variables strings and values. Let’s start by getting acquainted with the printenv command.

printenv

The printenv command will output all of your environment variables, and since the list of variables is long, it is recommended to pipe the results through the less utility.

printenv | less

For more amiable viewing, you have to pipe the list through both sort and less command.

printenv | sort -i | less

Note: you can navigate through the results by pressing Enter to check the next line or by hitting the space button to get to the next page. When you are done, click Q to exit the view.

We can also check for a specific environment variable:

printenv HOME
Output
/home/edxd

Note: printenv VAR is equivalent to echo $VAR when trying to output your variable’s value.

As you might have guessed, we can check for multiple environment variables with a single command line.

printenv HOME SHELL LOGNAME
Output
/home/edxd
/bin/bash
edxd

We can also utilize the grep tool to list only environment variables that contain character strings of our choosing.

printenv | grep edxd
Output
HOME=/home/edxd
USER=edxd
LOGNAME=edxd
PWD=/home/edxd
OLDPWD=/home/edxd
ZSH=/home/edxd/.oh-my-zsh

Similarly to printenv, the env command can also be used to display the environment variables. Note that we cannot pass a variable’s name to the env to print its value.

env | sort -i | less

Piped through grep:

env | grep edxd
Output
HOME=/home/edxd
USER=edxd
LOGNAME=edxd
PWD=/home/edxd
OLDPWD=/home/edxd
ZSH=/home/edxd/.oh-my-zsh

After we discovered how to list all environment variables using printenv and env, let’s check the set command which offers us the possibility to not only list all environment variables but also local and user-defined shell variables as well as shell functions.

set | sort -i | less

We can compare the set list against the printenv and env list to check the difference by piping the results through grep just like so:

set | grep "()"
Output
BASH_ALIASES=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=()
BASH_SOURCE=()
DIRSTACK=()
GROUPS=()
__expand_tilde_by_ref ()
__get_cword_at_cursor_by_ref ()

The echo command can output variable value. Check the mentioned below command:

echo $HOME
Output
/home/edxd
Note that all variable names must be prefixed with $ symbol, plus, it’s a best practice that the entire construct should be enclosed in quotes.

Also note that we can use the printf command.

How to Set Environment Variables

We’ve mentioned before that there are two types of variables: environment and local.

Environment variables are available shell-wide, which means child shell processes/subshells have access to these particular global variables, whether in a graphical interface or on the command line. We set up in these variables things like the path for the JAVA app, the default text editor, or our favorite web browser.

Local variables have a very limited scope. They are available only to the current shell instance, and they are non-persistent.

When creating either local or global variable, follow the mentioned below instructions:

You shouldn’t add space between the KEY, the = symbol, and the value.
KEY=value

Long values with spaces should be enclosed in quotes:

KEY="this value is long and spaced"

When a variable has more than one value, it must be separated by a colon ::

KEY=value1:value2

Note: It’s a best practice to write environment variables in all caps.

Let’s create a simple locally available variable.

MY_MOTTO="Feel the fear and do it any way"

Now let’s display it using the set command:

set | grep MY_MOTTO
Output
MY_MOTTO='Feel the fear and do it any way'

How about using the printenv command?

printenv | grep MY_MOTTO
Output
    

We have no output because the scope of this variable is, by default, the shell in which it’s defined.

Let’s check if this newly created variable is available for our shell’s child instance. We can achieve that by starting a new bash instance in our shell, then printing the variable’s value.

bash
echo $MY_MOTTO

As you can observe, echo returned an empty string confirming that this variable is indeed not recognized in the child process.

On a side note, maybe you think that bash command has no effect, but in reality, when executing bash, you have created a new instance that awaits your next command. Check the following set of commands:

exit
echo $MY_MOTTO
Output
Feel the fear and do it any way

We exited the subshell and returned to our parent shell, then printed out our local variable.

To make MY_MOTTO and its value available to other programs your shell invokes, execute the command below in the parent instance:

export MY_MOTTO

Check if it is available among the environment variables:

printenv | grep MY_MOTTO
Output
MY_MOTTO=Feel the fear and do it any way

Also, check if it is available in the subshell:

bash
echo $MY_MOTTO
Output
Feel the fear and do it any way

How to Unset Environment Variables

Now that we’ve learned how to create environment variables available to all subshells using the export command. How about removing such variables? Simply use the unset command and check the results as so:

unset MY_MOTTO
echo "$MY_MOTTO"
printenv | grep MY_MOTTO

Note: Variables stored in configuration files can also be removed from just the current shell instance.

How to Set Persistent Environment Variables

As you might have noticed, these exported variables do not like to stick around after you end your session in the terminal.

So the method of creating environment variables by executing the export command has a very short life, which means that everything you set up by executing the export command is session specific.

To make our environment variables permanent, we simply append whatever expand KEY=value command to our .bashrc or .bash_profile files.

Session Environment Variables (bashrc)

Just open you .bashrc file as you would with any text file:

nano ~/.bashrc

Scroll all the way down to the bottom. It’s recommended that you add comments to make things neat, then create a variable and export it just as in the previous example:

export MY_MOTTO="Feel the fear and do it any way"

Save and exit the .bashrc file, then source it for the changes to take effect in the current session:

source ~/.bashrc

Now start a new terminal session or even restart your machine, then check if MY_MOTTO is available among the environment variables:

printenv | grep MY_MOTTO
Output
MY_MOTTO=Feel the fear and do it any way

Also, check if it is available in the subshell.

bash -c "echo $MY_MOTTO"
Output
Feel the fear and do it any way.

Note: when appending the export command to your .bashrc file as demonstrated, it only put the availability of your variable under the current user. To change that and make the environment variable available to all users, append your export variable=value line to the /etc/bashrc.

Remote Login Sessions Environment Variables (bash_Profile)

Environment variables are accessible to remote login sessions (SSH), and we can achieve this effect by adding them to your .bash_profile file. As before, append the command used to create a locally defined environment variable to your bash profile file.

nano ~/.bash_profile
export MY_MOTTO="Feel the fear and do it any way"

Save and exit the .bash_profile file, then source it for the changes to take effect in the current session:

~/.bash_profile

Now start a new terminal session, then check if the availability of your environment variable through remote connections.

ssh edxd@localhost
echo $MY_MOTTO
Output
Feel the fear and do it any way

Note: Similarly for the .bashrc, appending your environment variables to your .bash_profile file will make your variable available on remote login sessions, only for the current user. To change that and make it available to all users, append your export variable=value line to the /etc/profile

To remove a permanent environment variable, just delete the corresponding line from either .bashrc or .bash_profile files.

Conclusion

In this article, we discussed the different ways of setting up environment variables by command line (temporary) and by appending the export command to the .bashrc/.bash_ profile files (permanent), as well as pointing out the way of making remote login session variables.

Additionally, we talked about how to unset or remove these variables, not to mention the different viewing commands to check for such variables. Knowing how to manage your environment variables will help you to use Linux more efficiently.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like