Every process has an environment associated with it. An environment
is a collection of environment variables. A variable is a changeable
value with a fixed name. For example, the name EMAIL could refer to
the value [email protected]. The value can vary; EMAIL could
also refer to [email protected].
Because your shell is a process like any other, it has an environment. You can
view your shell's environment by entering the printenv command.
Figure 6.1:
Sample printenv output
Figure 6.1 on page
has some sample output from printenv. On your system, the output will
be different but similar.
Environment variables are one way to configure the system. For example, the
EDITOR variable lets you select your preferred editor for posting news,
writing e-mail, and so on.
Setting environment variables is simple. For practice, try customizing your
shell's prompt and your text file viewer with environment variables. First,
let's get a bit of background information.
man less
This command lets you view the online manual for the less command.
In order to show you the text one screenful at a time, man invokes
a pager that shows you a new page of text each time you press the space
bar. By default, it uses the pager called more.
Go ahead and glance over the man page for
less, which is an enhanced pager. Scroll to a new page by pressing
space; press q to quit. more will also quit automatically
when you reach the end of the man page.
export PAGER=less
After reading about the advantages of less, you might want to use it
to read man pages. To do this, you set the environment variable PAGER.
The command to set an environment variable within bash always has this
format:
export NAME=value
export means to move the variable from the shell into the environment.
This means that programs other than the shell (for instance, a file
viewer) will be able to access it.
echo $PAGER
This is the easiest way to see the value of a variable. $PAGER tells
the shell to insert the value of the PAGER variable before
invoking the command. echo echoes back its argument: in this case,
it echoes the current PAGER value, less.
man more
Displays the more manual. This time, man should have invoked
the less pager.
less has lots of features that more lacks. For example, you
can scroll backward with the b key. You can also move up and down (even
sideways) with the arrow keys. less won't exit when it reaches the
end of the man page; it will wait for you to press q.
You can try out some less-specific commands, like b, to verify
that they don't work with more and that you are indeed using more.
unset PAGER
If you don't want to specify a pager anymore, you can unset the variable.
man will then use more by default, just as it did before you
set the variable.
echo $PAGER
Because PAGER has been unset, echo won't print anything.
PS1=hello:
Figure 6.2:
Changing the prompt
Just for fun, change your shell prompt. $ should now change; see Figure
6.2 for details.
export is not necessary, because you're changing the shell's own behavior.
There's no reason to export the variable into the environment for other programs
to see. Technically, PS1 is a shell variable rather than an
environment variable.
If you wanted to, you could export the shell variable, transforming
it into an environment variable. If you do this, programs you run from the shell
can see it.