Every time you start a shell session in Linux, the system goes through configuration files and sets up the environment accordingly. Environment variables play a significant role in this process.
In this tutorial, you will learn how to set, view, and unset environment variables in Linux.
What Are Environment Variables: A Definition
Environment variables are variables that contain values necessary to set up a shell environment. Contrary to shell variables, environment variables persist in the shell’s child processes.
Structurally, environment and shell variables are the same – both are a key-value pair, separated by an equal sign.
If a variable has more than one value, separate them with a semicolon:
Variables that contain spaces are written under quotation marks:
Note: The convention is to use all caps for writing variable names, in order to distinguish them among other configuration options.
Most Common Environment Variables
Here are some environment variables that an average user may encounter:
PWD– Current working directory.
HOME– The user’s home directory location.
SHELL– Current shell (bash, zsh, etc.).
LOGNAME– Name of the user.
UID– User’s unique identifier.
HOSTNAME– Device’s hostname on the network.
EDITOR– The Linux system default text editor.
TEMP– Directory location for temporary files.
How to Check Environment Variables
View All Environment Variables
printenv command to view all environment variables. Since there are many variables on the list, use the
less command to control the view:
printenv | less
The output will show the first page of the list and then allow you to go further by pressing Space to see the next page or Enter to display the next line:
Exit the view by pressing
Search a Single Environment Variable
To check a single environment variable value, use the following command:
printenv VARIABLE NAME
HOME variable value is the home folder path:
Alternatively, display the value of a variable by using the
echo command. The syntax is:
Search Specific Environment Variables
To find all the variables containing a certain character string, use the grep command:
printenv | grep [VARIABLE_NAME]
The search output for the
USER variable shows the following lines:
Another command you can use to check environment variables is
set. However, this command will also include local variables, as well as shell variables and shell functions.
Find an environment variable in the list that contains all the variables and shell functions by using
set | grep:
set | grep [VARIABLE_NAME]
Note: Take a look at our in-depth guide on how to use the Linux set command to learn more about it.
Set an Environment Variable in Linux
The simplest way to set a variable using the command line is to type its name followed by a value:
1. As an example, create a variable called
EXAMPLE with a text value. If you type the command correctly, the shell does not provide any output.
set | grep command confirms the creation of the variable. However,
printenv does not return any output.
This is because the variable created in this way is a shell variable.
3. Another way to confirm this is to type
bash and start a child shell session. Using the
echo command to search for the
EXAMPLE variable now returns no output:
Note: In a child process,
EXAMPLE is not an existing variable.
How to Export an Environment Variable
1. If you want to turn a shell variable into an environment variable, return to the parent shell and
export it with the Linux export command:
printenv to confirm the successful export:
3. If you open a child shell session now,
echo will return the environment variable value:
The environment variable created in this way disappears after you exit the current shell session.
Set an Environment Variable in Linux Permanently
If you wish a variable to persist after you close the shell session, you need to set it as an environmental variable permanently. You can choose between setting it for the current user or all users.
1. To set permanent environment variables for a single user, edit the .bashrc file:
sudo nano ~/.bashrc
2. Write a line for each variable you wish to add using the following syntax:
3. Save and exit the file. The changes are applied after you restart the shell. If you want to apply the changes during the current session, use the source command:
4. To set permanent environment variables for all users, create an
.sh file in the
sudo nano /etc/profile.d/[filename].sh
5. The syntax to add variables to the file is the same as with
6. Save and exit the file. The changes are applied at the next logging in.
How to Unset an Environment Variable
To unset an environment variable, use the
This command permanently removes variables exported through a terminal command.
Variables stored in Linux configuration files are also removed from the current shell session. However, they are set again upon next logging in.
To permanently unset a variable you stored in a file, go to the file, and remove the line containing the variable definition.
After reading this article, you should know how to set and unset environmental variables on a Linux system. Knowing how to set environment variables will help you more easily configure software packages in the future.