All you need to know about Unix environment variables

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

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

$ env | sort | head -10
HISTIGNORE=history:pwd:man *:ls:ls *
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

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

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:


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.

Join the Network World communities on Facebook and LinkedIn to comment on topics that are top of mind.

Copyright © 2017 IDG Communications, Inc.