List and Set Environment Variables in Linux

Written by: Linuxopsys   |   Last updated: April 25, 2022

The Linux operating system is widely used in servers to host complex applications, which have very specific system settings. When you start a new Linux shell session, a certain system configuration is read and your computer is set up accordingly. These configurations and settings are controlled by the environment variables.

In this guide, we will learn about how to list and set environment variables in a Linux system.


  • A Linux computer with terminal access.
  • Some familiarity with the Linux command-line interface.
  • Eagerness to learn advanced Linux configuration.

What are Environment Variables in Linux

The Linux operating system supports two types of variables- environment and shell variables. These variables can affect the way your applications and programs work.

Environment variables are system-wide variables that affect the entire system. They are used by all shells and spawned child processes, and are available on both command-line and GUI. Linux environment variables are dynamic system values that supply the necessary information to programs and processes. These variables control things like default web browser, default file editor or default text editor, executable files, keyboard layout settings, directory path, and system locale. Basically, those are names that have a value assigned to them. For example, we use JAVA_HOME variable to set the directory location where JDK or JRE was installed.

Shell variables are native to a particular shell instance that affect only the shell functions. Every Linux operating system shell, such as Bash and Korn has their own internal shell variables. This helps to track data in the current session - mostly used in an interactive shell session or a script to keep variables local. The Bash specific variables are normally prefixed with BASH_ for example BASH_VERSION.

Both environment and shell variables have the same format:


You must keep the following variable requirement in mind when working with both shell and environment variables:

  • The variable names by convention always are in uppercase, such as PATH.
  • Cases sensitive - it's possible to have a lower case.
  • There should not be a space before and after the = sign.
  • Always separate multiple values by a colon (:), for example, value1:value2:value3.

The following table lists some of the most common environment variables.

Variable NameDescription
PATHA colon-separated list of directories that Linux searches when executing a command.
USERUsername of the currently logged-in user.
HOMEHome directory of the current user.
UIDUnique identifier of the current user.
EDITORDefault editor to edit the files.
SHELLName of the current user’s shell.
TERMCurrent terminal emulation.
PWDPresent working directory.
TEMPPath of the temporary directory.

The values of some of these variables might be different on every Linux computer.

List Current Environment Variables

Linux has several variables and it is very important to know their values because they affect your system’s behavior. There are several commands available to list environment variables in Linux.

To print a list of all environment variables, type:

list of all environment variables using printenv

You can also use the env command to list all Linux variables:

print environment variables using env command

The list of variables can be very long, and thus it is difficult to read. You can use the more or less command along with the printenv command to list environment variables one screen at a time:

printenv | more

All the commands described above list multiple variables at a time. To find information about specified variable or variables, use the following command:

printenv HOME

This command prints the home directory of the user.

You may also use the echo command to print the environment variable value. The following example prints the values of the PATH environment variable:

echo $PATH

The PATH variable has multiple values and they are separated by a colon.

Set Environment Variables

Linux offers two ways to set environment variables. You can first set a shell variable and then export the shell variable as an environment variable, the second way is to directly export an environment variable.

To create new environment variables and set their value for the current shell session, use the following command:


At this point, new environment variables are created only for the shell. You can confirm if the variable is set using the echo command. This variable is not an environment variable and if you use the printenv command, its output will be empty:

printenv MYVAR

Now we can convert this shell variable to a single environment variable using the export command:

export MYVAR

Now MYVAR is an environment variable, this affects the current shell and all processes started from the current shell.

You do not have to always follow this long process to set an environment variable. You can directly export the environment variable in a single command:

export MYENV=bar

You must note that variables set through these commands are available only for the current session. Once you close the session, these variables are lost. To permanently set the environment variables, you must edit /etc/profile.d directory, ~/.bashrc, /etc/environment, or /etc/profile files using a text editor.

Set Environment Variables Persistent

Persistent means when you close your shell session or reboot, you don't need to reset the value of the environment variable. If you want an environment variable to be available across all shell sessions, and even across users, then you must add environment variables in configuration files.

Use the /etc/environment file to add environment variables that are available throughout your system (ie for all users, all processes) for command-line and graphical interface. This file only accepts variable in the format VAR_NAME="value", don't use export. For example, using your favorite editor open /etc/environment file, and add environment variables using the following format:


After setting logout from the current user and login again for the changes to take place.

Use the /etc/profile configuration file to set environment variables that are automatically loaded for all users when starting a login shell. Use the export command to declare environment variables in the user profile as follows:

export PATH=$PATH:/place/with/the/file

One user specific environment variables are set in the ~/.bashrc file or ~/.profile file, use as follows:

export JAVA_HOME=/opt/jdk/11
export PATH="$JAVA_HOME/bin:$PATH"

Run source ~/.bashrc the bash shell command to load the environment variables in the current shell session.

Unset Environment Variables

Use the unset command to unset or delete a variable.

unset NAME
unset environment variable $NAME

The unset command deletes variables that were exported using a terminal command. However, the variables that you stored in the configuration files are deleted only from the current shell session. When you create a new terminal session, they are set again automatically.

You can permanently delete variables from your Linux computer by deleting the variable from the configuration file.


In this guide, we learned how to list and create environment variables in Linux. We have also seen how to make variables persistent over reboot by editing bash configuration files.

Environment variables are a great way to set frequently used values and just use the variables instead of manually typing the values again and again. The common environment variables listed in this tutorial are available for most Linux distributions such as Ubuntu, Debian, Redhat, and CentOS Stream.



Please add comments below to provide the author your ideas, appreciation and feedback.

1 Comment

1 thought on “List and Set Environment Variables in Linux”

Leave a Comment