List Environment Variables on Linux

How to List Environment Variables on Linux

A Linux administrator needs to learn how to list environment variables. In this article, we will discuss how you can achieve this. So read on to find out more.

Types of Variables in Linux

In Linux, there are two types of variables: system variables and user-defined variables. System variables are known as environment variables, and they contain information restricted to the system. These values affect the programs and processes residing in the Linux machine. On the other hand, users can create and modify user-defined variables as they desire.  

Environment variables are case-sensitive, unlike user-defined variables. When Linux executes an environment variable, it displays the value stored in it. However, if the variable does not exist, it displays the “command not found” error. The environment variables look something like this:

KEY="Some other value"

The multiple values are separated by a colon (:). Also, there is no space before and after the “=” sign. 

Some Common Environment Variables in Linux

Here is a list of some widely used environment variables that Linus machines use:

  • PATH: contains a list of directories consisting of locations for executable files. The locations are separated by a colon “:”.
  • USER: stores the username of the currently logged-in user.
  • HOME: lists the default path to the user’s home directory.
  • EDITOR: stores the path to the file editor.
  • UID: lists the user’s unique ID.
  • SHELL: the current shell variant being used by the user.
  • BASH_VERSION: holds the bash version.
  • HOSTNAME: lists the name of your machine.
  • CDPATH: contains the location of the cd command.
  • HISTFILE: contains the location of the history command.
  • HISTFILESIZE: lists the maximum number of lines contained in the history file. 
  • HISTSIZE: saves the number of commands the history command should remember. The default value is 500 commands.
  • IFS: Stands for Internal Field Separator. It contains the default field separator for a file. The default values are <space><newline><tab>.
  • TMOUT: contains the default waiting time for the built-in read command. In other words, the number of seconds the shell should wait for the user input after executing the read command. 
  • TERM: The current terminal emulation.
  • MAIL: Location of where the current user’s mail is stored.

How to List Environment Variables on Linux 

You can use various easy methods to list environment variables. In this section, we will also discuss how you can set and modify and create your environment variables. Let’s look at each one in detail.

List All Environment Variables Using Set Command

To view all the environment variables that exist in the system, use the set command. First, open the terminal by pressing “Ctrl + Alt + T” and execute the command given below:


To view the list of environment variables in a separate window, execute:

set | more

You should get a similar output as the one seen below:

List Environment Variables

The set command displays all the variables in the VARIABLE=value pair. 

List All Environment Variables Using Echo Command

On the other hand, you can also use the env command to view the list of all environmental problems. Specifically, type:

env | more

List All Environment Variables Using Printenv Command

Linux shell can also process the environment variables using the printenv command. To use this command to print all environment variables, type:


For a particular environment variable, execute:

printenv LANG PWD

The output should look something like this:


List a Particular Environment Variable

Alternatively, you can view a specific system variable using the set command. You just have to type in the following command:

set HOME
set $PATH
set $BASH
set $USER

In addition, you can view it like this too:


Or this:

echo $HOME
echo $PATH 
echo $SHELL
echo $BASH
echo $USER
echo $LANG

Search for a Particular Environment Variable

To search for any variable using the terminal, use the grep command. Specifically, type:

printenv | grep $HOME
printenv | grep $PATH
printenv | grep $BASH
printenv | grep $USER

Create and Set Environment Variables Value

In addition to viewing the system-defined variables, you can also create your environment variables and set their value. The syntax looks something like this:

VARIABLE_NAME= variable_value

Make sure to create a variable name in upper case. For example:

NEW_VAR=’new value’

This will create a shell variable. You can confirm this by using the printenv command as shown below:

printenv MY_VAR

Alternatively, execute this command for the same purpose:

bash -c 'echo $MY_VAR'

From this, you can see that the variable is a shell variable, not an environment variable. Now to convert it into an environment variable, first export this variable. Specifically, type:

export MY_VAR

And check this by executing this command:

printenv MY_VAR

Now, if you run the bash command below, you will see the variable is an environment variable now. 

bash -c 'echo $MY_VAR'

In addition, you can perform the entire step in a single line, as shown below:

export MY_NEW_VAR="My New Var"

Set Persistent Environment Variables on Linux

However, these variables are not persistent. To elaborate, the variable will be lost once the current session is terminated. To make environment variables persistent, you will set them in the bash file. So that the variables are not lost when the current session is closed. The bash file is located in the /etc/environment

To list environment variables in the .bashrc file, use the export command. The syntax for the export command looks something like this:

VAR_TEST="Test Var"

For example:

export JAVA_HOME="/path/to/java/home"
export PATH=$PATH:$JAVA_HOME/bin
export PATH="$HOME/bin:$PATH"

Now to declare the variables in the .bashrc file, use the source command. Specifically, type:

source ~/.bashrc

Lastly, check whether the variable is an environment variable using the printenv command:

printenv MY_VAR

The output should look something like this:

Check persistent variable

In this article, we have looked at how to list the environment variables in Linux. In addition, we also discussed how to set shell variables as environment variables. Lastly, you learn how to create persistent environment variables. So hopefully, you now know how to list environment variables on Linux.

You can also check the PATH of the environment variables as well as create your Shell functions on Linux.

If this article helped you, please share it.

Leave a Reply
Related Posts