Lesson 21: The shell environment¶
The Linux environment is a collection of environment variables. An environment variable is a user-specific or system-wide object that contains data used by one or more applications. In simple terms, it is a variable with a name and a value.
Note
By convention, environment variables are capitalised.
Some frequently used environment variables include:
Environment variable Purpose
---------------------- ------------------------------------------------------------------------
`HOME` The home directory for the current logged in user
`USER` The username of the currently logged in user
`PWD` The current working directory, retrieved also with `pwd`
`PATH` A colon separated list of directories Linux looks for executable files
`LD_LIBRARY_PATH` A colon separated list of directories Linux looks for library files
Retrieving an environment variable¶
To retrieve a single variable, either one of the below commands will
yield the same result using printenv
or echo
followed by the
variable, for example:
[learner planets]$ printenv PATH
/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
[learner planets]$ echo $PATH
/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
Notice how the $
character is included in the second command. This
tells the shell to interpret the argument (PATH
) as a variable and not
a literal. Try executing the command without the $
character!
To retrieve all environment variables, just type printenv
.
Setting an environment variable¶
To create (or change) an environment variable, we assign a value to the variable using the equals operator. As an example, to update the PATH variable to include directory /home/learner, the following command will be executed:
[learner planets]$ export PATH=$PATH:~
[learner planets]$ echo $PATH
/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/home/learner
We included $PATH
in our new PATH
variable as we want to append the
directory ~
to the existing path. If $PATH
was not included, PATH
would only contain ~
. Knowing how to change your $PATH
to include
custom directories can be necessary sometimes (e.g. if you install some
new software in a non-standard location).
Take care when updating $PATH
because removing system paths will cause
the shell to start displaying 'No such file or directory' errors as it
cannot find the location where programs such as cd
and ls
are
installed. If this occurs, restart your shell to fix the issue.
If the environment variable does not already exist on the system (you
can check with printenv
), you will need to export
your variable to
the environment, for example:
[learner planets]$ export PLANETSDIR=~/learning_linux/planets
Note
Changing environment variables will only work in the current shell.
To permanently make changes, add the export
commands to your
~/.bashrc
file.
The bashrc file is a shell script that Bash runs when it is started
interactively. It can contain commands to initialise the environment
(export
commands), or assign command
aliases.