Skip main navigation

£199.99 £139.99 for one year of Unlimited learning. Offer ends on 28 February 2023 at 23:59 (UTC). T&Cs apply

Find out more

Environment and Shell variables In Linux

We discuss the differences between environment and shell variables in Linux and provide some examples, as well as discuss how to implement them.
© Wellcome Genome Campus Advanced Courses and Scientific Conferences

In computer operating systems, an environment is an area that contains information about the behaviour of programs and applications.

Linux environment variables are used by applications to get information about the environment, and each environment variable is a variable with a name and an associated value.

Every time the environment is configured a new shell session is created, and this can be used as a learning experience to see how changing Linux environment variables can change things like the appearance of the shell, create paths to executable files, keyboard layout settings, and defining the default home directory for example.

Variables have the following format and by convention have upper case names. Though they are case-sensitive, so it is possible to have lower case names. Also, there is no space around the equals = symbol.

KEY=value KEY=”Another value”

If you assign multiple values to a variable then separate them with a colon : character.


There are two types of variables:

Environment variables are system wide and are inherited by all system processes and shells.

Shell variables only apply internally to the current shell instance.

How do we use Linux commands to list and set environment variables?

This can be achieved by using the following commands.

env – This 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 – This prints all the specified environment variables. For example, to display the value of the HOME environment variable type:

printenv HOME


This will print out the path of the currently logged in user.




You can also pass more than one argument to the printenv command, in the form:


printenv <argument1><argument 2>


Try this example


printenv LANG PWD


This will produce an output similar to this one.




Running printenv without any arguments will show a list of all the environment variables.




This will produce an extensive output similar to the truncated one below.


LESSCLOSE=/usr/bin/lesspipe %s %s


Some of the more common environment variables are highlighted below.



Command Description
printenv USER Current logged in user
Printenv HOME Home directory of the current user
Printenv EDITOR The default editor
SHELL The path of the current user’s shell
LOGNAME The name of the current user
PATH A list of directories to be searched when executing commands
LANG The current locales settings
TERM The current terminal emulation
MAIL Location of where the user mail is stored


set – This sets or unsets shell variables. If used without an argument then it will print a list of all variables, both shell and environment, and shell functions.


Type set in a terminal and it will produce a long list of all the variables.


BASH_VERSINFO=([0]="4" [1]="4" [2]="20" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu")


unset – This deletes shell and environment variables.


export – This command sets environment variables.




Below are a few examples highlighting the difference between Environment and Shell variables. We’ll first create a Shell variable and then transform it into an Environment variable.


Create a new variable called MY_VARIABLE and give a value of Linux_Variable.




Verify the variable exists by typing:




This will display:




Use the printenv command to check whether MY_VARIABLE is an environment variable or not. If the output is empty then it is not an environment variable.


printenv MY_VARIABLE


This will display no output.


Now we have our Shell variable, we can transform it into an Environment variable by typing:




Check MY_VARIABLE exists by typing


printenv MY_VARIABLE


This time, the variable will now be displayed as it is an Environment variable:


Please note that variables created in this manner will only be available in the current session, and will be lost when you log out or open a new shell.


In order to make the Environment variables persistent you’ll need to add them to specific Linux files.


Edit the following file using the nano editor.


nano /etc/environment


Add MY_VARIABLE=value to a new line and Save. Define your value accordingly. This could be text or a number for example.


Another useful method, if you are using Bash, is to declare your variable in the hidden .bashrc profile file.


nano ~/.bashrc


Add in a new line to the .bashrc file


export MYVARIABLE=” value”


Save and update the changes to the .bashrc file by typing


source ~/.bashrc
© Wellcome Genome Campus Advanced Courses and Scientific Conferences
This article is from the free online

Bioinformatics for Biologists: An Introduction to Linux, Bash Scripting, and R

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education