What are environment variables?

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 editor or 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.

Environment Variables and Shell Variables 

Variables have the following format:

KEY="Some other value"
  • The names of the variables are case-sensitive. By convention, environment variables should have UPPER CASE names.
  • When assigning multiple values to the variable they must be separated by the colon :character.
  • There is no space around the equals = symbol.

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:

  • env – The command allows you to run another program in a custom environment without modifying the current one. When used without an argument it will print a list of the current environment variables.
  • printenv – The command prints all or the specified environment variables.
  • set – The command sets or unsets shell variables. When used without an argument it will print a list of all variables including environment and shell variables, and shell functions.
  • unset – The command deletes shell and environment variables.
  • export – The command sets environment variables.

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:


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

printenv LANG PWD

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


The output will look something like this:

LESSCLOSE=/usr/bin/lesspipe %s %s
SSH_CLIENT= 34422 22
LESSOPEN=| /usr/bin/lesspipe %s

Below are some of the most common environment variables:

  • USER - The current logged in user.
  • HOME - The home directory of the current user.
  • EDITOR - The default file editor to be used. This is the editor that will be used when you type edit in your terminal.
  • SHELL - The path of the current user’s shell, such as bash or zsh.
  • LOGNAME - The name of the current user.
  • PATH - A list of directories to be searched when executing commands. When you run a command the system will search those directories in this order and use the first found executable.
  • LANG - The current locales settings.
  • TERM - The current terminal emulation.
  • MAIL - Location of where the current user’s mail is stored.

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 functions you can use the set command:


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

set | less

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


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.

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:

  • /etc/environment - Use this file to set up system-wide environment variables. Variables in this file are set in the following format:

    FOO=barVAR_TEST="Test Var"
  • /etc/profile - Variables set in this file are loaded whenever a bash login shell is entered. When declaring environment variables in this file you need to use the exportcommand:

    export JAVA_HOME="/path/to/java/home"export PATH=$PATH:$JAVA_HOME/bin
  • Per-user shell specific configuration files. For example, if you are using Bash, you can declare the variables in the ~/.bashrc:

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

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

source ~/.bashrc


Popular Posts