Environment variables are a set of dynamic named values in Linux, used by applications launched in shell. It allow you to customize how the system works and the behavior of the applications. For example, the environment variable can store information about the default browser or the path to executable files.

This article discuss about working with environment variable. We will dive into

  • Setting variable
  • Unsetting variable
  • Exporting variable

Environment and Shell Variable

Environment variables are variables that are available system-wide and are inherited by all child processes and shells. Shell variables are variables that apply only to the current shell instance.

Commands to work with 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 – Prints all or the specified environment variables.
  • set – 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 – Deletes shell and environment variables.
  • export – Sets environment variables.

List Environment Variable

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

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:

Most common environment variables are:

  • USER – Current logged in user.
  • HOME – Home directory of the current user.
  • EDITOR – Default file editor to be used. This is the editor that will be used when you type edit in your terminal.
  • SHELL – 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.

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.

Setting Environment Variable

Following example shows how to create a new shell variable MY_VAR.


echo $MY_VAR command can show the value of this variable. The names of the variables are case-sensitive. When assigning multiple values to the variable they must be separated by the colon : character. There is no space around the equals(=) symbol.

Now, let’s turn our shell variable into an environmental variable. We can do this by exporting the variable. The command to do so is appropriately named:

export MY_VAR

This will change our variable into an environmental variable. We can check this by checking our environmental listing again:

printenv | grep MY_VAR

Variable set 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. 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.
  • /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 export command:
  • ~/.bashrc – Per-user shell specific configuration files.

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

source ~/.bashrc

Unsettling Environment Variable

We still have our MY_VAR variable defined as an environmental variable. We can change it back into a shell variable by typing:

export -n MY_VAR

Now it is no longer an environmental variable. Below command will show not empty result.

printenv | grep MY_VAR

However, it is still a shell variable. If we want to completely unset a variable, either shell or environmental, we can do so with the unset command:

unset MY_VAR

We can verify that it is no longer set using echo. Nothing is returned because the variable has been unset.

echo $MY_VAR<br>