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 /bin/bash
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
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
List environment variables
printenv command can be used to list all environment variables on a Linux system.
To list a specific variable, just pass the name of it to the command.
$ printenv SHELL /bin/bash
You can also check multiple variables simultaneously.
$ printenv HOME SHELL /home/linuxconfig /bin/bash
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 /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
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
- 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.
- Next, use the
exportcommand to set the new variable as an environment variable.
$ export MY_SITE
- 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
/etc/profile configuration files by appending a line to the end of it with this syntax. Notice we precede each new variable with
Afterwards, you can load the new environment variables into the current session with the following command.
$ source ~/.bashrc OR # source /etc/profile
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.
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.