How to Set and List Environment Variables in Linux

Linux TLDR
Last Updated:
Reading time: 5 minutes

The environment variables are a set of key-value pairs that define the shell environment and can affect the behavior of the ongoing programs that are executed in the current shell session.

The programs running in the current shell session can access the variable values from the shell or subshell, and you can set them for various purposes.

For example, customizing the user preferences, shrinking long commands into short ones, making binary programs accessible by adding their paths to the environment variables, and many more.

Tutorial Details

DescriptionEnvironment Variables
Difficulty LevelLow
Root or Sudo PrivilegesNo
Host System and ArchitectureUbuntu 22.10 (x64)
OS Compatibility Ubuntu, Manjaro, Fedora, etc.
Prerequisitesenv, printenv, echo, grep, set, unset, source, export
Internet RequiredNo
Discussed Tools in this Articleawk, vim, nano

Scope of the Environment Variables in Linux

There are two types of environmental variables:

  1. Global Environment Variable
  2. Local Environment Variable

Global Environment Variable

These variables are persistent variables that can be accessed from anywhere; you can even use them in your shell script.

These variables with their values are stored either in the “~/.bash_profile” or “~/.profile” or “~/.bash_login” or “~/.bashrc” files according to the requirement.

Also Read: Difference Between /etc/profile, ~/.bash_profile, ~/.profile, ~/.bashrc, etc

Local Environment Variable

These variables are temporary variables, which means they are accessible until the current session is over. After the session is terminated, all the local variables will be flushed.

Common Environment Variables in Linux

The following is a list of known environment variables that you might already be using:

Environment VariablesDescription
PATHIt stores the multiple directory absolute paths separated using the “:” colon that are used to look for while executing any command from the terminal.
USERIt stores the currently logged-in user’s username.
HOMEIt stores the path to the current logged-in user’s home directory.
EDITORIt stores the absolute path of the editor that will be used by default.
UIDIt stores the current logged-in user’s UID.
TERMDefine the terminal type.
SHELLIt stores the current logged-in user’s default shell absolute path.
PWDDisplay the current working directory.
LOGNAMEIt stores the name of the logged-in user.
LANGIt stores the default system language.

Execute the following command to get a complete list of environment variables and their values in your system.

$ env

You can also ignore the values from the output by only printing the environment variables (keys) using the awk command.

$ env | awk -F "=" '{print $1}'

How to Check Existing Environment Variables in Linux

As you learned previously, the env command can help you find your existing environment variables; however, there are a few more ways to list them, followed by:

CommandsDefinition
envIt is used to create a fresh set of environment variables or replace the existing ones. If specified without arguments, then it will list the existing environment variables in your system.
printenvIt will print all the existing environment variables on your system or the value of the specified variable.
echo $keyAppend the “$” at the beginning of the variable name (or key) to fetch their value.

Using the env Command

The env command will list the current environment variables in your system:

$ env

Output:

Env command output
Env command output

Using the printenv Command

The printenv command will also list the present environment variables in your system:

$ printenv

Output:

Printenv command output
Printenv command output

To get the value of the specific variable, specify its name (or key) as an argument to the printenv command:

$ printenv LOGNAME

Output:

Printing the LOGNAME variable using the printenv command
Printing the LOGNAME variable using the printenv command

Another example,

$ printenv SHELL

Output:

Printing the LOGNAME variable using the printenv command
Printing the LOGNAME variable using the printenv command

Using the echo $key

Append the β€œ$” at the beginning of the variable name (or key) to fetch their value using the echo command.

$ echo $LOGNAME

Output:

Printing the LOGNAME variable using the echo command
Printing the LOGNAME variable using the echo command

Another example,

$ echo $SHELL

Output:

Printing the SHELL variable using the echo command
Printing the SHELL variable using the echo command

Setting Environment Variables

As we discussed earlier, environment variables have multiple scopes: global and local.

We will first take a look at local variables:

Setting Local Environment Variable

The easiest way to set local environment variables is by directly specifying the key-value pair in your terminal, as shown.

$ site=linuxtldr.com

Note that you cannot directly fetch the value of this variable from its variable name using the env or printenv commands.

However, you can use the set command and pipe the grep command along with the key name to get the variable value, as shown.

$ set | grep site

Output:

Creating and printing the local environment variable
Creating and printing the local environment variable

Note that the above method of creating variables will only print the value in the parent shell where the variable was created.

If you create a sub-shell by running the bash command in your current shell and try to fetch the value of the same variable, you will get null.

#Parent Shell
$ set | grep site
site=linuxtldr.com

#Creating Sub-Shell
$ bash

#Unable to get the value of the variable
$ set | grep site
$ exit
exit

#Exiting Sub-Shell
$ set | grep site
site=linuxtldr.com

Output:

Accessing a local variable within the subshell
Accessing a local variable within the subshell

However, you can use the export command while creating the variables to make them accessible inside the sub-shell.

$ export site=linuxtldr.com

The good thing about specifying export at the beginning while creating environment variables is that you are able to fetch variable values using the env or printenv commands.

$ echo $site
$ printenv site

Output:

Creating and printing the variable using the export command
Creating and printing the variable using the export command

And you are also able to access the variable values inside the sub-shell.

#Parent Shell
$ printenv site
linuxtldr.com

#Creating Sub-Shell
$ bash
$ printenv site
linuxtldr.com
$ exit
exit

Output:

Accessing the variable created using the export command within the subshell
Accessing the variable created using the export command within the subshell

To remove/delete this variable, specify the variable name (or key) as an argument to the unset command.

$ unset site

Output:

Removing the variable
Removing the variable

Setting Global Environment Variable

The previous method of creating environment variables will not persist the variable after the session is closed or restarted.

To make it accessible even after that, specify these variables inside your shell configuration file.

But first, you have to find your default shell and its configuration file path.

Also Read: Listing All the Available Shells in Your Linux System

Execute any of the following commands to find your existing default shell.

$ printenv SHELL

#OR

$ echo $0

Output:

Checking the default login shell
Checking the default login shell

The following is a list of the configuration file locations for known shells.

ShellConfiguration File Location
bash shell~/.bash_profile” or “~/.bash_login” or “~/.bashrc” or “~/.profile
zsh shell~/.zshrc
fish shell~/. config/fish/config. fish

Note: Consider putting all the variables in “~/.bash_profile” (or “~/.profile“) if you need them in an interactive login shell (it will be only executed once when you log in to your system); otherwise, add them in “~/.bashrc” to execute them in an interactive non-login shell (it will be executed on an already logged-on system or when you create a sub-shell).

For example, if Bash is your default shell, then edit the “~/.bashrc” file using your choice of text editor, like Vim or Nano.

$ vim ~/.bashrc

#OR

$ nano ~/.bashrc

And then add your environment variables at the end of the configuration file, as shown.

Adding an environment variable inside the bash configuration file
Adding an environment variable inside the bash configuration file

Save and close the configuration file, then source it to reflect the changes.

$ source ~/.bashrc

Output:

Sourcing the bash configuration file
Sourcing the bash configuration file

Lastly, test the environment variable by fetching the values using the variable name.

$ echo $site

#OR

$ printenv site

Output:

Checking the global "site" variable
Checking the global “site” variable

Note that these variables are also accessible within the sub-shell.

#Parent Shell
$ echo $site
linuxtldr.com

#Creating Sub-Shell
$ bash
$ echo $site
linuxtldr.com
$ exit
exit

Output:

Accessing the global site variable within the subshell
Accessing the global site variable within the subshell

To remove this variable from your system, you need to edit the shell configuration file (referring to the “~/.bashrc” file) and remove the previously added line, then save and source the configuration file to reflect the changes.

That was all you needed to know about environmental variables.

If you have any questions or queries related to this topic, then feel free to ask them in the comment section.

Till then, peace!

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.