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.
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
I get:
jimmo
Typing
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 in the chapter "Introduction to Operating Systems".
It is very common that users' shell prompt is defined by the systems. 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 |
\n |
newline |
\r |
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>
|