How to Set, List, and Use Environment Variables on Linux

This guide will show you how to set and list environment variables on Linux-based systems.

Environment Variables on Linux

Environment variables are a crucial part of the Linux system and store values that help define the current environment. One of the environment variables that you may be familiar with is the PATH variable.

These environment variables are accessible easily through the shells or subshells and can be used to make using the shell easier and more consistent. For example, instead of finding the directory where temporary files are stored, you can keep that path in a variable called “TEMP“.

In the next couple of sections, we will teach you a couple of things about environment variables on Linux. The first thing we will be covering is how you will need to format these variables when you define them.

We also show you how to set these environment variables so that they will stay persistent across new sessions. This persistency is achieved by setting these variables within some essential configuration files.

The Format of Environment Variables on Linux

The format for environment variables is relatively straightforward, so we will only be quickly going over them.

Let us show you three different examples of how you can define environment variables within Linux.

1. Our first example shows the most basic way of defining an environment variable.


At the very least, you need the variable’s name, followed immediately by the equals sign (=), followed by the value you want to be stored in it.

There should be no spaces between the variable name, the equals sign (=), and the value you are assigning.

When naming your environment variable, you should always use capital letters.

2. For our following example, we will be showing how you would write an environment variable if you wanted to include spaces.

VARIABLE="A Value With Spaces"

When using spaces in your value, you should wrap at in double quotation marks (" ").

The reason for this is that spaces do not typically play well with shell interfaces like Bash. Whitespace space often signifies the start of a new attribute within the terminal.
3. Our third and final example will quickly show how you can define multiple values to a single environment variable.


To assign multiple values to a single environment variable, you need to separate each value by using the colon symbol (:). An excellent example of an environment variable that uses multiple values is “PATH“.

Setting an Environment Variable on Linux

Now that you understand the format of an environment variable on Linux, let us show you how to set them.

There are a few different files on Linux systems that you can modify to set an environment variable. EachThese files behave slightly differently but are all typically loaded when a new terminal session is started.

Below we will go through four of these files and explain the differences between them.

Within each example, we will define an environment variable called “WEBSITE” that will have the value of ““. We will also set a variable called “TUTORIAL” to “Linux Tutorial“.

/etc/environment” – This file will allow you to set environment variables that will be available system-wide.

When defining variables in the environment file, you can use the standard format as covered in the previous section.
TUTORIAL="Linux Tutorial"

/etc/profile” – Using the profile file, you can set system-wide environment variables that are loaded in whenever a login shell is entered.

When you define an environment variable in this file, you must preface it with the “export” command.

export TUTORIAL="Linux Tutorial"

~/.profile” – This is the user-specific version of the above file. Instead of being system-wide these environment variables will only be set when that specific user logs in.

Most shells will read in values specified within this file. It is not specific to any shell system.

Like with the system-wide profile file, you must use the “export” command when defining the environment variable.

export TUTORIAL="Linux Tutorial"

~/.bashrc” – This is a per-user file that affects the Bash shell interface explicitly. If your system is using Bash, you can use this file to define the environment variable.

Ensure that when you define an environment variable within this file, you use the “export” command.

export TUTORIAL="Linux Tutorial"

Once you have set new environment variables, the easiest way to ensure these values are available is to log out and back into your account. Doing this ensures that all of your user’s processes will receive these new variables.

Reloading Environment Variables on Linux

If you only need your environment variables to be available to the current terminal session, then it is possible to reload the files without logging out and back in.

To do this, we will be using a special command called “source” that is available as a part of bash and most other popular shells.

We need to use “source” followed by the path to the file we want to read in.

For example, if we updated our user’s profile file to add a new environment variable, we can run the following command.

source /etc/profile

There are a couple of downsides to doing this.

  1. The first is that the changes will only be available to the current shell and any new applications launched from it.

    For older processes to get these new variables, they will need to log out and back in.
  2. The second downside is that you can not load in the “/etc/environment” file.

    This file is a list of assignments and not an executable script like the other files we mentioned, so bash can’t read it as a source.

Listing Environment Variables on a Linux System

Now that you have an idea of setting an environment variable on Linux, let us quickly show you how to list them.

The most straightforward command to use to list what environments variables are available in the current session is “printenv“.


Without using any arguments, the “printenv” command will print out all environment variables currently available in this terminal session

It is also possible to use this command to print out the value of a specific variable. All you need to do is reference the name of the variable after “printenv“.

For example, if we want to get the value of the PATH environment variable, we can use the following command.

printenv PATH

Finally, you can also use a tool such as grep to filter the list provided by “printenv” to look for variables matching a specific pattern.

For example, if we wanted to list all environment variables that contain the word “pi“, we can run the following command.

printenv | grep pi

Using a Variable in the Terminal

Our final section will show you how it is possible to use these variables within the Linux terminal.

We won’t be delving into this too far for this guide, as they are relatively easy to use within the terminal.

You need to know that when using an environment or shell variable within the terminal, you must use the dollar sign symbol ($) before the variable name.

For example, if we wanted to echo the “PATH” environment variable, we would use the command below.

echo $PATH

Another example is to use the “HOME” variable to start editing a file within our user’s home directory.

nano $HOME/examplefile.text

As you can see, all we need to do to reference these variables is to make sure we use the dollar sign symbol ($) in front of them.


With this guide, you should now understand how to set, list, and use environment variables on Linux.

Environment variables have a wide variety of uses, especially when dealing with the terminal. Importantly they help you create a consistent environment across multiple sessions.

If you have any issues with this guide, please feel free to leave a comment below.

Be sure to check out some of our other Linux tutorials, and check out our Ubuntu guides.

Leave a Reply

Your email address will not be published. Required fields are marked *