Shell Variables

Shell Variables

The shell’s environment is all the information that the shell will use as it runs. This includes such things as your command search path, your logname (the name you logged in under), and the terminal type you are using. Collectively, they are referred to as your environment variables and individually, as the “so-and-so” environment variable, such as the TERM environment variable, which contains the type of terminal you are using.

Note that not every variable is a “environment variable”. Any shell script can set variables as it pleases, but they have no effect on the environment. Typically variables are referred to as environment variables if the variable is accessible directly from the shell. You might hear the term more strictly used to define variable that are defined by one of the respective shell’s configuration files.

When you log in, most of these are set for you in one way or another. (The mechanism that sets all environment variables is shell-dependent, so we will talk about it when we get to the individual shells.) Each environment variable can be viewed by simply typing echo $VARIABLE. For example, if I type

echo $LOGNAME

I get:

jimmo

Typing

echo $TERM

I get:

ansi

In general, variables that are pre-defined by the system (e.g. PATH, LOGNAME, HOME) are written in capital letters. Note that this is not a requirement as there are exceptions.

Note that shell variables are only accessible from the current shell. In order for them to be accessible to child processes (i.e. sub-processes) they must be made available using the export command. In the system-wide shell configuration file or “profile” (/etc/profile) many variables, such as PATH are exported. More information on processes can be found in the section on processes.

It is very common that users’ shell prompt is defined by the system. For example, you might have something that looks like this:

PS1=’\u@\h:\w> ‘

What this does is to set the first level prompt variable PS1 to include the username, hostname and the current working directory. This ends up looking something like this:

jimmo@linux:/tmp>

Adding the \A to display the time, we end up with something that looks like this:

10:09 jimmo@linux:/tmp>
Variable Meaning
\u Username
\h Hostname
\H The fully-qualified hostname
\w Current working directory
\d date
\t the current time in 24-hour HH:MM:SS format
\T the current time in 12-hour HH:MM:SS format
\@ the current time in 12-hour am/pm format
\A the current time in 24-hour HH:MM format
\l the basename of the shell’s terminal device
\e Escape character
newline
carriage return

One way of using the escape character in your prompt is to send a terminal control sequence. The can be used, for example, to change the prompt so that the time is shown in red:

PS1=’\e[31m\A\e[0m \u@\h:\w> ‘

Which then looks like this:

10:09 jimmo@linux:/tmp>

There a several variables that are special for the shell. For example, $$ refers to the PID of the shell currently running. This is commonly used to the PID into a file, which can be checked later, for example to verify the process is still running. So, you might have something like this:

PID=$$

The next special variable is $? and contains the status of the last command executed in the foreground. This is often used for things like if-statements when you wnat to check whether a command successfully completed before continuing. For example:

grep SOMEDATA filename STATUS=$?

This would store the exit code of the grep command.

The variable $! contains the process ID of the last process executed in the background. This is a good way of keeping track of multiple processes, which are all run in the background.

You will find that on most system quite a few variables are set by default. You can display all of them at once simply by issuing the shell-builtin set. Note that this will also show you functions, as well as variables. You can also issue the env command, which just shows you the environment variables.

Note this these two commands do not show you the same variables. I think it easiest to think that set will show you all of the variables that have been set, whereas env will only show you variables that are part of your environment. That it, it will only show you variables that have been exported. That is, it is possible to set a variable and not have it appear in the output of the env command.