List of Linux Environment Variables and How to Set Them

linux set environment variable

Linux environment variables are a unique feature of the OS that stores values about the system’s various parameters. Experienced users, in particular, can set a value for these variables to speed up workflows.

By setting these variables, you can optimize application performance and designate specific apps and parameters for these applications. In fact, you can use these variables to provide starting arguments and parameters to the shell, subshell, and various apps you launch through the GUI.

This tutorial covers the basics of Linux environment variables, how you can view these variables, and, most importantly, how you can easily set new values for these variables. The good thing about these variables is that you can use them with any Linux distro without worrying about conflicts.

Let’s start with an introduction to Linux environment variables.

What Are Linux Environment Variables?

Environment variables are the variables defined in a particular environment.

In the context of Linux distributions, environment variables are commonly used within the Bash shell, mainly to set up the shell environment. Known as global variables, these allow you to customize how the system works and control the startup behavior of the applications on the system.

On the other hand, local variables are restricted and accessible from within the shell in which they’re created and initialized.

Generally, Linux environment variables are available globally and system-wide. As such, the subshells and child processes can also access these variables.

Structurally, Linux environment variables have a key-value pair structure, separated by an equal sign. Note that the names of the variables are case-sensitive, and they should be in upper case.

VARIABLE_NAME=value

If a variable has multiple values, they are separated by a semicolon:

VARIABLE_NAME=value_1:value_2

In case you need to include a space in the value of a variable, you should enclose it with quotation marks. However, traditionally, the variable name includes an underscore for better readability:

VARIABLE_NAME="Value text"

Now that you have a basic understanding of what Linux environment variables are, here’s a list of the popular environment variables.

The Most Common Environment Variables

Linux environment variables store values that help the system and the applications discover user preferences. Here’re some standard globally available environment variables that store important information about user preferences and options.

PATH: This variable contains a colon-separated list of folders where the system should look for system and user files.

HOME: The location of the logged-in user’s home directory.

SHELL: The location (path) of the shell the logged-in user prefers.

ENV: Contains the list of all environment variables.

UID: Contains the unique identifier of the logged-in user.

TEMP: Contains the path of the folder for temporary files.

This list shows that environment variables contain essential information about how the user has set up the system for their use. This includes the location of their files and system files

How To List Environment Variables In Linux

For starters, it is important that you know how to find out the environment variables and their values. For this, you can opt for several ways to list Linux environment variables.

printenv

This simple command lists the values of the current environment variables in the terminal. When used as is without any arguments, it lists all the variables.

[[email protected] ~]# printenv
XDG_VTNR=7
XDG_SESSION_ID=c1
SHELL=/bin/bash
TERM=xterm
USER=root
SUDO_USER=vyom
SUDO_UID=1000
USERNAME=root
MAIL=/var/mail/root
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
...

If you wish to find out the details of a specific variable, you can use the printenv command with the name of the variable as an argument. For instance, if you want to know the value of the HOME variable, you can use the printenv HOME command:

[[email protected] ~]# printenv HOME
/root
[[email protected] ~]# printenv HOME MAIL
/root
/var/spool/mail/root
[[email protected] ~]#

If you are not sure of the exact name of the variable, you can use grep to find the likely candidates. For instance, you can use this combination to find information about the SSH related variables. As you can see below, using grep with the printenv command prints the list of all variables that contain SSH in them.

[[email protected] ~]# printenv |grep SSH
SSH_CLIENT=x.x.x.x 1592 2xx5
SSH_TTY=/dev/pts/3
SSH_CONNECTION=x.x.x.x 1592 x.x.x.x 2xx5
[[email protected] ~]#

env

The env command is often used to quickly check the environment variables set for the current session.

[email protected]:~# env
SHELL=/bin/bash
PWD=/root
LOGNAME=root
HOME=/root
LANG=C.UTF-8
LESSOPEN=| /usr/bin/lesspipe %s
USER=root
SHLVL=1
XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
MAIL=/var/mail/root
_=/usr/bin/env

Since env could result in a long list of variables, finding the variables you’re looking for could take time. In this case, you can always use the command with grep to filter down the list to the variables you’re looking for.

Here’s how you can use env with grep to find out the SSH related variables:

[[email protected] ~]# env |grep SSH
SSH_CLIENT=x.x.x.x 1592 2xx5
SSH_TTY=/dev/pts/3
SSH_CONNECTION=x.x.x.x 1592 x.x.x.x 2xx5

set

The main reason why many power users don’t prefer env or printenv is because of their limited scope, where they print just the environment variables. On the other hand, the set offers a much broader scope and prints out the complete list of environment and shell variables and functions. As a result, the set can generate a long list spanning multiple screens.

set variable

We recommend using the less command to view the output one screen at a time.

# set | less

echo

echo is a common command that is used to list the value of shell variables in the terminal. Remember that you need to use the $ sign before the name of the variable.

[[email protected] ~]# echo $SHELL
/bin/bash

declare

declare is another popular utility for listing and setting a shell variable. When used without any arguments, it prints a long list of shell and environment variables. We suggest using the less command to easily view the list.

declare variable

Now that you know how to check Linux environment variables, let’s go into the details of how to set the Linux environment variables

How to Set Environment Variables in Linux?

Setting up environment variables is pretty easy. We can use the export command for setting up a new environment variable in the following format.

export NAME=VALUE

For example, the following command will set the Java home directory.

# export JAVA_HOME=/usr/bin/java

Note that you won’t get any response about the success or failure of the command. As a result, if you want to verify that the variable has been properly set, we’ll use the echo command.

echo $JAVA_HOME

If the variable has been properly set, the echo command will display the value. If the variable has no set value, you might not see anything on the screen.

It’s important to understand that any variable you set with the export command will only remain for the duration of the session. Once the session terminates for any reason, the variables you set will become “unset” and won’t be available in the subsequent sessions.

Let’s see how we can make the variables “persist” beyond the current session.

Make Environment Variables “Persist”

As mentioned earlier, variables you set during a session expire once the session terminates. This often annoys users because they expect variables to be always available after they set these variables.

To make an environment variable persistent for a user’s environment, you need to edit the .bashrc file:

sudo nano ~/.bashrc

Add an entry for each variable you wish to be always available. Remember to follow the proper syntax to avoid any surprises:

export [VARIABLE_NAME]=[variable_value]

Once done, save and exit the file.

Next, restart the shell so that the latest values for the variables are loaded.

You should know that when a new session starts, the variables can be read from multiple locations (for instance, from /etc/environment and /etc/profile. You can choose the source of the variables with the source command. For instance, if you wish to load variables for the bash shell, use the source common with the following argument.

source ~/.bashrc

The next step is to verify that the variables have been properly set and are available for use. For this, we’ll use the echo command.

# echo $JAVA_HOME
/usr/bin/java

Persisting Global Environment Variables for All Users

To make a permanent environment variable persist after a reboot, you need to add it to the default profile. All users on the system, including the service accounts, load this default profile.

All global profile settings are located under the/etc/profile. And while you can edit this file directly, we recommend storing the global environment variables in a directory called /etc/profile.d, where you can find a list of files being used to set environment variables for the entire system.

Let’s create a new file under /etc/profile.d to store the global environment variable. The name of the file should be contextual so that others may understand its purpose easily.

For example, we will create a permanent environment variable for JAVA_HOME.

sudo vi /etc/profile.d/java_home.sh

Add the below lines to export the variable.

export JAVA_HOME=/usr/bin/java

Save your changes and exit.

How to Unset an Environment Variable

To unset an environment variable, we can use the unset command:

unset [VARIABLE_NAME]

This command permanently removes variables exported through the terminal.

Variables stored in configuration files will also be removed from the current shell session. However, they will fetch again upon next logging in.

To permanently unset a variable you stored in a file, go to the file, and remove the entry containing the variable definition.

Conclusion

This tutorial covered setting and unsetting environment variables for all Linux distributions. You also learned how to make environment variables persistent for a single user and all users. An environment variable is a dynamic entity that stores specific values to configure the system.

Let us know what Linux environment variables you use and how you set them.