On Linux, knowing how to set and unset environment variables is essential to manage your hosts.
Environment variables are a set of key value pairs stored on your Linux and used by processes in order to be able to perform specific operations.
One very popular example is when you are trying to set the timezone on your Linux system.
You may need to set environment variables in order for your system time to change.
Environment variables can also be used in shell programs or in subshells in order to perform various operations (for example knowing if the current user is root or not).
In this tutorial, we are going to see how you can set, unset and list all the environment variables on your system. We are also going to have a look at specific files that can help you setting environment variables easily.
Set Environment Variables on Linux using export
On Linux, you have many different ways of setting your environment variables depending on if you want to make them persistent or not.
The easiest way to set environment variables is to use the export command.
$ export VAR="value"
Using export, your environment variable will be set for the current shell session.
As a consequence, if you open another shell or if you restart your system, your environment variable won’t be accessible anymore. If you need persistent environment variables, you can refer to the next section.
To get the value of your environment variable, you can use the “printenv” command with the variable you are looking for.
$ printenv <variable>
For example, given the example cited above, you would run the following command.
$ printenv VAR value
Alternatively, you can use Linux pipes in order to get the value of your environment variables.
You can also use the echo command but you need to have a dollar sign before the variable name.
$ echo $VAR value
Set Persistent Environment Variables on Linux
As you saw in the previous section, environment variables were not persistent over shell restarts.
However, there is a way to make your changes persistent : by using system files that are read and executed on specific conditions.
Using the .bashrc file
The most popular way to set environment variables persistently is to add them to the “.bashrc” file.
The “.bashrc” file is a script executed whenever you initialize an interactive shell session.
As a consequence, when you launch a new Terminal via the GNOME interface or if you simply use a screen session, you are going to use the .bashrc file.
For example, add the following entries to your .bashrc file.
$ export TZ="America/New_York"
Save your file and use the source command to reload the bashrc file for your current shell session.
$ source ~/.bashrc
You can print your new environment variable with “printenv” and see how your date was set on Linux by modifying TZ.
$ printenv TZ America/New_York $ date Sat 19 Oct 2019 10:03:00 AM EDT
Great, your changes are now persistent over shell or system reloads.
Alternatively, if you plan on connecting to your sessions via “login shells”, you can also add your environment variables directly into the .bash_profile file.
$ export TZ="America/New_York" $ source ~/.bash_profile
In the previous section, we have seen how you can set environment variables for users on your system.
However, you may want to enforce specific environment variables for everyone. In this case, you are going to define system-wide environment variables.
To set system wide environment variables on Linux, you need to export your variables in the /etc/environment file.
For example, to change the editor used globally, you can modify the EDITOR variable in the environment file.
$ export EDITOR="vi"
Now, try to login as different users on your system, and you will see that the EDITOR variable is set for everybody on the server.
[email protected]:~$ printenv EDITOR vi
Great! You have successfully set your environment variables persistently on Linux.
Set Environment Variables in one line
Now that you know the details on how to set environment variables, you can use those shortcuts in order to set them easily.
# One line for .bashrc $ echo "export VAR="value"" >> ~/.bashrc && source ~/.bashrc # One line for bash_profile $ echo "export VAR="value"" >> ~/.bash_profile && source ~/.bash_profile # One line for /etc/environment $ echo "export VAR="value"" >> /etc/environment && source /etc/environment
Unset Environment Variables on Linux
Now that you have set many environment variables on your system, you may want to unset some of them if you don’t use them anymore.
On Linux, there are two ways of unsetting environment variables : by using the unset command or by deleting variable entries into your system files.
Using unset command
To unset an environment variable, use the unset command with the following syntax
$ unset <variable>
Given the examples cited above, you would run those commands
$ unset USERNAME $ printenv USERNAME <nothing>
Using the set -n command
Alternatively, you can unset environment variables by using the set command with the “-n” flag.
$ set -n USERNAME $ printenv USERNAME <nothing>
Common Set of Environment Variables on Linux
Now that you know how you can set and unset environment variables on Linux, it is time to have a look at the common set of environment variables that you can find on your system.
- USER : the current username of the user using the system;
- EDITOR : the program run to perform file edits on your host;
- HOME : the home directory of the current user;
- PATH : a colon separated list of directories where the system looks for commands;
- PS1 : the primary prompt string (to define the display of the shell prompt);
- PWD : the current working directory;
- _ : the most recent command executed on the system (by the user)
- MAIL : the path to the current user’s mailbox;
- SHELL : the shell used in order to interpret commands on the system, it can be many different ones (like bash, sh, zsh or others);
- LANG : the language encoding used on the system;
- DESKTOP_SESSION : the current desktop used on your host (GNOME, KDE)
- HISTFILESIZE : number of lines of command history stored in the history file;
- HISTSIZE : number of lines of history allowed in memory;
- UID : the current UID for the user
Set PATH environment variable on Linux
On Linux systems, it is very common to set the PATH environment variable in order for the system to be able to locate commands.
To display your current PATH environment variable, run the printenv command
$ printenv PATH /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
To set the PATH environment variable, add an export line to your .bashrc file and source it.
$ echo "export PATH="<path>:$PATH"" >> ~/.bashrc && source ~/.bashrc $ printenv PATH <path>:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
You have successfully updated your PATH environment variable on Linux.
In today’s tutorial, you learnt that you can set environment variables on Linux in multiple ways : with the export command but also by modifying some system files to make them persistent.
You also learnt that it is possible to unset environment variables and how to update your PATH environment variable easily.
If you are interested in Linux System Administration, we have plenty of tutorials on the subject in our dedicated category.