Environment variables are part of the Linux system shell that contain changing values. They help facilitate scripts and system programs, so that code can accommodate a variety of scenarios. Unlike regular shell variables, environment variables can be accessed system-wide, by any user or process.

Let's look at a very simple example to see how environment variables work and why they exist. There are many system programs and user made scripts that need to access a current user's home directory. This can be done reliably through the HOME environment variable. Therefore, a script that contains the following line can be used by any user on the system and it will generate the same result.

$ echo $SHELL > $HOME/current-shell.log

$ cat $HOME/current-shell.log

In this guide, we'll show how to list all the environment variables on a Linux system, as well as set new ones. Setting new environment variables can either be done temporarily, or permanently if you need them to survive a reboot. We'll show instructions for both methods below.

In this tutorial you will learn:
  • How to list environment variables on Linux
  • How to set a temporary environment variable on Linux
  • How to set a permanent environment variable on Linux
Listing environment variables
Listing environment variables
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Bash shell
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ - requires given linux commands to be executed as a regular non-privileged user

List environment variables

The printenv command can be used to list all environment variables on a Linux system.

$ printenv
Printing all the environment variables used by this Linux system
Printing all the environment variables used by this Linux system

To list a specific variable, just pass the name of it to the command.

$ printenv SHELL

You can also check multiple variables simultaneously.

$ printenv HOME SHELL

To interact with the environment variables in your terminal or when writing a script, you will need to precede them with a dollar sign $.

$ echo "I am logged in as $USER with the $SHELL shell and my home directory is $HOME"
I am logged in as linuxconfig with the /bin/bash shell and my home directory is /home/linuxconfig

A popular environment variable to edit is the $PATH variable, which lets you specify the directories Bash should search for programs when you enter a command. We've written a separate guide on how to add a directory to $PATH.

$ printenv PATH

Set a temporary environment variable

Here's how to create a new environment variable on Linux. Note that this is a temporary environment variable and won't survive a system reboot, user logout, or new shell. As an example, we'll create a new variable called MY_SITE.

  1. Use the following command to create a new shell variable. This will only make the variable active in your current session, but we will make an environment variable soon.
  2. Next, use the export command to set the new variable as an environment variable.
    $ export MY_SITE
  3. Alternatively, we can set the temporary environment variable by using a single command with this syntax:
    $ export MY_SITE="linuxconfig.org"

Set a permanent environment variable

In order to configure a new environment variable to be persistent, we'll need to edit the Bash configuration files. This can be done through three different files, depending on exactly how you plan to access the environment variable.

  • ~/.bashrc - Variables stored here will reside in the user's home directory and are only accessible by that user. The variables get loaded any time a new shell is opened.
  • /etc/profile - Variables stored here will be accessible by all users and are loaded whenever a new shell is opened.
  • /etc/environment - Variables stored here are accessible system-wide.

Add a new variable to the~/.bashrc or /etc/profile configuration files by appending a line to the end of it with this syntax. Notice we precede each new variable with export.

export MY_SITE='linuxconfig.org'

Afterwards, you can load the new environment variables into the current session with the following command.

$ source ~/.bashrc
# source /etc/profile
Loading a new environment variable and testing it with the printenv command
Loading a new environment variable and testing it with the printenv command

If adding an environment variable to the /etc/environment file, you don't need to precede the line with "export".


Using the methods above, your variable configurations will persist until you delete them.

Closing Thoughts

In this guide, we saw how to set and list environment variables on Linux. Environment variables are a useful convention in Linux shells that help facilitate system and user scripts.

FIND LATEST LINUX JOBS on LinuxCareers.com
Submit your RESUME, create a JOB ALERT.
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
Get extra help by visiting our LINUX FORUM or simply use comments below.