How to Set and List Environment Variables in Linux (original) (raw)

In Linux and Unix based systems environment variables are a set of dynamic named values, stored within the system that are used by applications launched in shells or subshells. In simple words, an environment variable is a variable with a name and an associated value.

Environment variables allow you to customize how the system works and the behavior of the applications on the system. For example, the environment variable can store information about the default text editoror browser, the path to executable files, or the system locale and keyboard layout settings.

In this guide, we will explain to read and set environment and shell variables.

Variables have the following format:

KEY=value
KEY="Some other value"
KEY=value1:value2

Variables can be classified into two main categories, environment variables, and shell variables.

Environment variables are variables that are available system-wide and are inherited by all spawned child processes and shells.

Shell variables are variables that apply only to the current shell instance. Each shell such as zsh and bash, has its own set of internal shell variables.

There are several commands available that allow you to list and set environment variables in Linux:

List Environment Variables

The most used command to displays the environment variables is printenv. If the name of the variable is passed as an argument to the command, only the value of that variable is displayed. If no argument is specified, printenv prints a list of all environment variables, one variable per line.

For example, to display the value of the HOME environment variable you would run:

printenv HOME

The output will print the path of the currently logged in user:

/home/linuxize

You can also pass more than one arguments to the printenv command:

printenv LANG PWD
en_US
/home/linuxize

If you run the printenv or env command without any arguments it will show a list of all environment variables:

printenv

The output will look something like this:

LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35;...
LESSCLOSE=/usr/bin/lesspipe %s %s
LANG=en_US
S_COLORS=auto
XDG_SESSION_ID=5
USER=linuxize
PWD=/home/linuxize
HOME=/home/linuxize
SSH_CLIENT=192.168.121.1 34422 22
XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop
SSH_TTY=/dev/pts/0
MAIL=/var/mail/linuxize
TERM=xterm-256color
SHELL=/bin/bash
SHLVL=1
LANGUAGE=en_US:
LOGNAME=linuxize
XDG_RUNTIME_DIR=/run/user/1000
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
LESSOPEN=| /usr/bin/lesspipe %s
_=/usr/bin/printenv

Below are some of the most common environment variables:

The printenv and env commands print only the environment variables. If you want to get a list of all variables, including environment, shell and variables, and shell functionsyou can use the set command:

set
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:login_shell:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()

The command will display a large list of all variables so you probably want to pipe the output to the lesscommand.

set | less

You can also use the echo commandto print a shell variable. For example, to print the value of the BASH_VERSION variable you would run:

echo $BASH_VERSION
4.4.19(1)-release

Setting Environment Variables

To better illustrate the difference between the Shell and Environment variables we’ll start with setting Shell Variables and then move on to the Environment variables.

To create a new shell variable with the name MY_VAR and value Linuxize simply type:

MY_VAR='Linuxize'

You can verify that the variable is set by using either echo $MY_VAR of filtering the output of the set command with grep set | grep MY_VAR:

echo $MY_VAR
Linuxize

Use the printenv command to check whether this variable is an environment variable or not:

printenv MY_VAR

The output will be empty which tell us that the variable is not an environment variable.

You can also try to print the variable in a new shell and you will get an empty output.

bash -c 'echo $MY_VAR'

The export command is used to set Environment variables.

To create an environment variable simply export the shell variable as an environment variable:

export MY_VAR

You can check this by running:

printenv MY_VAR
Linuxize

If you try to print the variable in a new shell this time you will get the variable name printed on your terminal:

bash -c 'echo $MY_VAR'
Linuxize

You can also set environment variables in a single line:

export MY_NEW_VAR="My New Var"

Environment Variables created in this way are available only in the current session. If you open a new shell or if you log out all variables will be lost.

Persistent Environment Variables

To make Environment variables persistent you need to define those variables in the bash configuration files. In most Linux distributions when you start a new session, environment variables are read from the following files:

export PATH="$HOME/bin:$PATH"  

To load the new environment variables into the current shell session use the sourcecommand:

source ~/.bashrc

Conclusion

In this guide, we have shown you how to set and list environment and shell variables.

Feel free to leave a comment if you have any questions.