How to Set and List Environment Variables in Linux?

Linux environment variables is a dynamic-named value that one or more running programs may use. Generally, building an environment is a set of defined rules and values. They help programs in many situations to check which directory installs files, which location stores temporary files, and many others.

The aim of this post is to explain the possible ways to set and list the environment variables. The guidelines of this post are written below:

Let’s start with setting environment variables.

How to Set Environment Variables?

The environment variables can be easily set on the Linux OS system temporarily, permanently, or globally. The environment variables are set with the help of the “export” command. The “export” allows the command line variables to be available to a child process.

Set the Environment Variables Temporary

The “temporary” environment variables are set for an instance of time. It remains in the system till the current user is “logged in” to the system. It will automatically delete when the user logs out of the system.

To set the environment variable temporarily, use the export command in the following way:

$ export TESTVAR=12345

The “TESTVAR” environment variable has been set.

To check whether that environment variable is set or not, use the “printenv” or “env” command:

$ printenv TESTVAR

The output verifies that the “TESTVAR” variable is set with a key value “12345”. It will delete automatically when the current user logs out from the system.

Set the Environment Variables Permanently

Set the environment variable permanently first, access the “.bashrc” file, and open it in the text editor (nano or vi). In this case, we opened the “.bashrc” file in the “nano” text editor via the below-mentioned command:

$ nano .bashrc

The above command will pop up the “vi” text editor window containing the “.bashrc” file:

Scroll down the file and add the following “export” commands at the end of the file.

export SAMPLEVAR=‘abcd’



The above “export” commands will set the environment variable “SAMPLEVAR” with a key value “abcd” permanently:

Save the changes (Ctrl+S) and exit the editor (Ctrl+X).

Now restart the Linux system and run the “printenv” command to verify the existence of the “SAMPLEVAR” environment variable:

$ printenv SAMPLEVAR

The key value of “SAMPLEVAR” on the terminal verifies that it is set permanently.

Tip: Instead of restraining the system reload the “.bashrc” file using the command:

$ source .bashrc

Set the Permanent Environment Variables Globally

In the above two sections, the environment variable is set for the current user. However, the other user of the system can not access it.

Note: Only the root user can set the global variable permanently.

To set the permanent environment variable for all users, i.e., globally, then create the “” new script in the “/etc/profile.d/” directory using the “nano” text editor as a root user:

# nano /etc/profile.d/

After that add the new global environment variable “NEWVAR” via the “export” command in this way:

export NEWVAR=4567

Save and exit the file.  After that reload the file by using the “source” command for its updation:

# source /etc/profile.d/

Logout from the root user using the “exit” command. Now logged in as an “anna” (or other) user and access the “NEWVAR” global environment variable:

# export $NEWVAR

The “anna” user has exported the “NEWVAR” global environment variable.

How to List Environment Variables?

All the possible ways are discussed in this section to list the environmental variables.

List All Environment Variables

Linux offers the “printenv” or “env” command to list down all the current working system environment variables. The practical implementation of both these commands is shown below:

$ printenv

The output displays the current Linux OS system’s “Ubuntu 22.04” predefined “environment variables”.

$ env

The output of the “env” command is the same as the “printenv” command displayed.

List Only Specific Environment Variables

The “printenv” command is also beneficial to view the particular environment variable. Moreover, this task can also be performed by using the “echo” command in this way:

$ echo $HOSTNAME

The above command shows the variable “HOSTNAME” key value “ubuntu”.

Now, execute the “printenv” command with the specified variable name as shown below:

$ printenv USER

The output denotes the “USER” variable key value which is “itslinuxfoss

Display Detail Information of Environment Variables

The above commands only provide the list of all or specific environment variables, not their complete information. Use the “set” command that will display the complete information of the environment variable that includes “environment”, “shell functions”, “variables” and “shells”:

$ set

The above output contains a large list of information. To get less information at a time using the set and  “less” command joined via the “| pipe” character:

$ set | less

Now press the “Enter” key as it provides the detailed information of environment variables line by line.

That’s how you can set and list environment variables in Linux.


In Linux the environment variables are set using the “export” command line tool. These variables can be set permanently or globally for all Linux users. Once they are set the user can view them by utilizing the “printenv”, “env”, or the “echo” commands. This post has illustrated all the possible command line tools to set and list the environment variables.