Home Learn Linux How to Set and List Environmental Variables in Linux

How to Set and List Environmental Variables in Linux

by Nitish.S
set and list environmental variables (1)

In Linux, environment variables are dynamic named-values that function as a placeholder for stored information in the system used by command-line applications. Just like any other variable, it consists of two parts – a name and an associated value.

Here is a quick look at the formatting of variables:

KEY="this is also a value"

A few things to note:

  • There should be no space before or after the “=” symbol.
  • If the values contain white spaces, then quotations are used.
  • You can assign multiple values to a variable. Each value must be separated by a colon (:) character.
  • The names of the variables are case-sensitive. As such, “KEY” and “key” denotes two different variables. By convention, an environment variable should be written in UPPER CASES.

Using Environment variables, you can potentially customize how your system works, including the behavior of the applications installed on your system.

For example, you can use environment variables to store information about default applications like a text editor, browser, or even the path to an executable file. This allows you to modify the system according to your personal needs or the needs of a group of users.

Now, for this read, we have put together a detailed tutorial discussing how to set and list environmental variables on your Linux PC. This will give you a workable understanding of using environment variables to personalize your system.

So without further ado, let’s get started:

What is the Difference Between Environment Variables and Shell Variables?

Before we get started, let’s first understand the differences between an environment variable and a shell variable.

To put it simply, an “environment variable” is globally available and is inherited by any spawned child processes or shells.

Shell variables, on the other hand, are variables that only apply to the current shell instance. Bash, zsh, and all other shells have their own set of internal shell variables. These are generally used to keep track of temporarily relevant data, like the current working directory.

Now, you can make a shell variable available as an environment variable. All you need to do is use the “export” command along with the variable name. This will export its value globally so that new programs and subshells can now use it.

Here is an example of how it is done:

exporting shell variables to environmental variables

Exporting shell variables to environmental variables

We will go more in-depth into this in a subsequent section.

How to List and Set Environment Variables

There are tons of commands available to help you list and set variables. Here is a quick look at some of the most common commands that we will be using in this tutorial:

  • export: used to export the value of a newly assigned environment variable.
  • env: used to run another program in a custom environment without needing to modify the current one. It can be used with an argument to print a list of all the present environment variables in the shell.
  • printenv: used to print all (or specified) environment variables and definitions of the current environment.
  • set: used to assign or define an environment or shell variable. If used without an argument, it will print a list of all the variables – this includes environment variables, shell variables, and shell functions.
  • unset: used to delete a specified environment or shell variable.

Using these commands, you will be able to list and set environment variables on your Linux system.

Listing Environment Variables

The most common command used to display the environment variables is “printenv”.

If you use the name of a variable as the argument for the command, then it will display the value of the variable. However, if used without an argument, it will list all the environment variables on the system.

Here are a few examples:

let’s say you want to see the value of the “HOME” environment variable. To do this, you need to run the following command:

$ printenv HOME

This will give you the path of the currently logged in user:

Value of the HOME variable

Value of the HOME variable

You can also pass two or more arguments through the “printenv” command as follows:

$ printenv PWD LANG

You will get a similar output screen:

Value of the PWD and LANG variable

Value of the PWD and LANG variable

Now, if you run “printenv” without an argument, then it will display a list of all environment variables.

list of all environment variables

List of all environment variables

Now, the printenv and env commands only display the environment variables. But, what if you want to list all the variables on your system? This includes everything starting from environment variables to shell variables and even shell functions.

Well, then you can use the “set” command without any argument:

list of all variables

List of all variables

As you can see in the image, the command will display a large verbose list of all the variables. If you want to pipe down the output, you can use this command instead:

$ set | less

Besides all of this, you can also use the echo command if you want to print the values of the shell variables. Just enter the echo and pass the concerned shell variable as its argument. Here is an example, using it with the BASH_VERSION variable.

using echo to display value of a shell variable

Value of a shell variable

To help you experiment with these commands on your system, here is a list of some of the most command environment variables.

  • EDITOR – The default file editor to be used when you type in the editor in the terminal.
  • HOME – The home directory of the current user.
  • LANG – The current locales settings.
  • LOGNAME – The name of the current user.
  • LS_COLORS – Defines all the color codes used to add the optional colored output to the ls command. These colors are used to distinguish between different file types and make the output more easily digestible.
  • MAIL – The Location of where the current user’s emails are stored.
  • PATH – A list of directories to be searched when executing commands. When you run a command, the system will search these directories in the listed order and use the first found executable.
  • PWD – Location of the current working directory.
  • OLDPWD – Location of the previous working directory.
  • SHELL – The path of the current user’s shell, e.g., bash or zsh.
  • TERM – The current terminal emulation.
  • USER – The current logged in user.

Now let’s talk about how to set the environment variables.

Setting Environment Variables

The common practice for creating an environment variable is to make a shell variable and then export it as an environment variable.

Now to create a new shell variable, we use the following command:


You can now use the echo command to verify the variable is set with the provided value.

$ echo $MY_VAR

Similarly, use printenv to see if the variable is an environment variable. If not, you will get an empty output.

$ printenv MY_VAR

Also, check to see if the variable is inherited by the sub-shell using the following command:

$ bash -c 'echo $MY_VAR'

Here too, you will get an empty output as shown in the image below:

setting a shell variable and verifying it

Setting a shell variable

Now that we have set the MY_VAR shell variable and verified it, we can use the export command to set it as an environment variable.

$ export MY_VAR

Once done, you can use the previous commands to see if it’s listed as an environment variable and see if it’s inherited by the sub-shell.

setting an environment variable and verifying it

Setting an environment variable

You can also set an environment variable using a single line, as shown below:

$ export NEW_VAR="This is a new variable"

However, do note that environment variables created in this way are only available for the current session. Once you log out or open a new shell, the environment variable will be lost.

Persistent and Non-persistent Environment Variables

As discussed earlier, when you modify the environment variables in your current shell, it will be lost as soon as you log out from the shell. This is called non-persistent environment variables.

To make them persistent, or in other words, permanent, you will need to edit the bash configuration files.

When you start a new session, all the environment variables are read from the following files. As such, you can add the environment variables to these files to make them persistent.

  • /etc/environment: This file contains all the system-wide environment variables. You can add new variables using the following format:
  • /etc/profile: This file includes variable sets that are loaded when a bash login shell is entered. You can declare an environment variable in this file using the export command.
$ export JAVA_HOME="/path/to/java/home"
$ export PATH=$PATH:$JAVA_HOME/bin
  • You can also set environment files in per-user shell specific configuration files. For example, if you are using Bash, you can declare the variable inside ~/.bashrc using the following command:
$ export PATH="$HOME/bin:$PATH"

To load the new environment variable in the current shell session, you will need to use the source command as follows:

$ source ~/.bashrc

Once you go through these steps, all the environment variables that you just added will become permanent and will remain intact even if you log out from the shell.


So this was our in-depth guide on how to list and set environment variables in Linux. We hope you found this helpful and educational. In case you have some questions or run into any problems, leave us a comment, and we will get back to you with a quick reply.

If you start with Linux, you can visit our Learn Linux series, where we cover in-depth tutorials and guides that will benefit both beginners and seasoned Linux users.

You may also like

Leave a Comment



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us


©2016-2023 FOSS LINUX



“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.