Notes on Linux: Environment Variables
Environment variables are a pretty fundamental aspect of working with Linux, and are often assumed knowledge in blog posts and tutorials. But they’re also one of those things that are so basic they can get overlooked. I have to admit I’ve been able to survive using Linux for a while now without really knowing how to use and manipulate them. So, this is me taking the time to understand them better.
The environment is the configuration that is set up every time a new session is started on your computer, and defines how the system operates.
A variable is a way of representing or storing information. This is usually
achieved using a
KEY and a corresponding
VALUE. In the case of a traditional
dictionary the word or phrase is the key, and it’s meaning is the value. With
this we can easily access meanings through their corresponding words.
So, environment variables are pieces of information made available to your computer operating system in order to configure it appropriately.
Linux environment variables are stored in the following format, where it is possible to store multiple values, separating them with a colon.
Accessing Environment Variables
To access our environment variables we can use the
commands. Along with environment variables
set displays shell variables, shell
functions and local variables.
printenv are very similar, displaying
the same information, though with
printenv you are able to access specific
variables, and with
env you can provide specific variables to a command in
order to temporarily modify the environment for that command.
Above is the output of
printenv, piped through
less for brevity (the
is from my prompt rather than part of the command). To access specific values
printenv with the key we want.
% printenv HOME /home/rob
And we can temporarily alter our environment with
env, placing it in front
of the command we want to provide the environment variable for, in this case
~ 13:35 % printenv HOME /home/rob ~ 13:35 % env HOME=/another/path printenv HOME /another/path ~ 13:35 % printenv HOME /home/rob
~ 13:35 is just the time in my prompt rather than anything to do
with the command)
We can also access environment variables with the
echo command, though when we
do this we have to include a
$ to indicate it is a variable rather than the
~ 13:40 % echo HOME HOME ~ 13:40 % echo $HOME /home/rob
Setting & Unsetting Variables
To set an environment variable we can use the same
KEY=value format to assign
a value to a key…
~ 13:45 % printenv TERM rxvt-unicode-256color ~ 13:45 % TERM=xterm ~ 13:45 % printenv TERM xterm ~ 13:45 % export TERM
Setting this only applies to the current shell session, it won’t apply to any
other terminal sessions, so we have to
export the variable. Setting this
overwrites the current value of the variable, which is not always what we want
to do. For instance we may need to add a new directory to the
a list of all the directory locations the system will look for commands in. To
append a new location we can do the following…
~ 13:55 % printenv PATH /home/rob/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/go/bin:/home/rob/code/go/bin ~ 13:55 % PATH=$PATH:/home/rob/code ~ 13:55 % printenv PATH /home/rob/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/go/bin:/home/rob/code/go/bin:/home/rob/code
We use the current value of
PATH as part of the new value, adding on our new
value to the end, separating the two with a colon, and assign it back to the
PATH variable. We can now see it at the end of our PATH. Again we would need
export this to make it available to all shell sessions.
As well as system environment variables, we can also create and assign user-defined variables…
~ 14:01 % printenv SOME_KEY ~ 14:01 C:1 % SOME_KEY=some_value ~ 14:01 C:1 % export SOME_KEY ~ 14:01 % printenv SOME_KEY some_value
These can often be created for specific program requirements.
To unset an environment variable we can use the
~ 14:41 % printenv KEY ~ 14:41 C:1 % export KEY=value ~ 14:41 % printenv KEY value ~ 14:41 % unset KEY ~ 14:41 % printenv KEY
Persisting Environment Variables
These environment variable changes will not survive a system restart and we
don’t want to have to type them in every time we start our system. So we have to
include them in a configuration file that is run every time we boot up. On bash
this could be
there’s plenty of discussion online about where to keep environment variables. I
use ZSH and have environment variables in both my
./zshrc I set the
LANG variable and the
export LANG=en_GB.UTF-8 export TERM=rxvt-unicode-256color
~/.zshenv I set my
PATH variable and the value of my
user-defined variable used by the Go programming language.
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games" export PATH=$HOME/bin:$PATH # GOPATH export GOPATH=$HOME/code/go
I’ve had a few ohhh, that’s why that is like that moments while learning about Linux environment variables. It feels good to properly understand what’s going on. The links below have been super helpful…