Americas

  • United States
sandra_henrystocker
Unix Dweeb

All you need to know about Unix environment variables

How-To
Aug 14, 20173 mins
LinuxUnix

Environment variables help to configure your account so that it’s easier to use, but they can also be changed to suit your needs and preferences.

Simply put, environment variables are variables that are set up in your shell when you log in. They are called “environment variables” because most of them affect the way your Unix shell works for you. One points to your home directory and another to your history file. One identifies your mail file, while another controls the colors that you see when you ask for a file listing. Still another sets up your default search path.

If you haven’t examined your environment variables in a while, you might be surprised by how many of them are configured. An easy way to see how many have been established in your account is to run this command:

$ env | wc -l
25

The env command (or printenv) will list all of the enviroment variables and their values. Here’s a sampling:

$ env | sort | head -10
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
HISTCONTROL=ignorespace
HISTFILE=/home/shs/.history
HISTIGNORE=history:pwd:man *:ls:ls *
HISTSIZE=100
HISTTIMEFORMAT=%h %d %H:%M:%S>
HOME=/home/shs
LANG=en_US.UTF-8
LESSCLOSE=/usr/bin/lesspipe %s %s
LESSOPEN=| /usr/bin/lesspipe %s

In this sampling of settings, we see a signalling setting, settings that control how many previous commands are displayed when you use the history commands, what commands are ignored, and how history information is formatted. We also see some locale data, as well as settings for the less command.

You can display a single variable with a simple echo command.

$ echo $HISTFILE
/home/shs/.history

You can create a variable on the command line by using a command like “myvar=11”, but it’s not really an environment variable unless you also export it and it won’t be available in subshells. If, instead, you typed “export myvar=11”, the variable will then also be available if you initiate a subshell.

Environment variables will be remembered only if you make them permanent (as far as “permanent” goes on a Unix system) by adding them to one of your startup files — like .~/bashrc, ~.profile or ~/.login.

You can get rid of environment variables fairly easily with the unset command, but remember to remove them from the file in which they were set up initially. If you remove a variable that you need, you might have some interesting consequences.

$ unset HOME
$ cd /tmp
$ cd
-bash: cd: HOME not set

Fortunately, reversing the change is just as easy.

$ export HOME=/home/shs
$ cd
$ pwd
/home/shs

Some environment variables are set up in system files like /etc/profile that are read before your local setup files when you log in.

Environment variables can be changed or unset, but they can also be augmented. If your search path is set up in /etc/profile, you can redefine it in your local .profile or add to it with a command like this:

PATH=~/bin:$PATH:/apps/bin

In this case, your personal bin directory (~/bin) would then be searched before other directories on your path and /apps/bin would be searched last provided it isn’t also included in the initial $PATH.

Environment variables help to configure your account so that it’s easier to use, but they can also be changed to suit your needs and preferences.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.