system. The
shell is also your interactive interface to your system. When you first
start a shell, it does some automated housekeeping to get ready for your
use, and then presents a command prompt. The command prompt tells you
that the shell is ready to accept commands from its standard
input device, which is usually the keyboard. Shells can run
standalone, as on a physical terminal, or within a window in a GUI
environment. Whichever the case, their use is the same.
Shell variable basics
During execution, bash maintains a
set of shell variables that contain information
important to the execution of the shell. Most of these variables are
set when bash starts, but they can be set
manually at any time.
The first shell variable of interest in this topic is calledPS1, which simply stands for Prompt String
1 . This special variable holds the contents of the command
prompt that are displayed when bash is ready to
accept commands (there is also a PS2 variable, used when bash needs multiple-line input to complete a
command). You can easily display the contents of PS1, or any other
shell variable, by using the
echo
command with the
variable name preceded by the
$
symbol:
$ echo $PS1 \$
The
\$
output tells us that
PS1 contains the two characters
\
and
$
. The backslash character
tells the shell not to interpret the dollar symbol in any special way
(that is, as a metacharacter , described later in
this section). A simple dollar sign was the default prompt for sh , but bash offers options
to make the prompt much more informative. On your system, the default
prompt stored in PS1 is probably something like:
[\u@\h \W]\$
Each of the characters preceded by backslashes has a special
meaning to bash , whereas those without
backslashes are interpreted literally. In this example,
\u
is replaced by the username,
\h
is replaced by the system’s hostname,
\W
is replaced by the unqualified
path (or basename) of the current working directory, and
\$
is replaced by a
$
character (unless you are root , in which case
\$
is replaced by
#
). This yields a prompt of the form:
[adam@linuxpc adam]$
How your prompt is formulated is really just a convenience and
does not affect how the shell interprets your commands. However,
adding information to the prompt, particularly regarding system, user,
and directory location, can make life easier when hopping from system
to system and logging in as multiple users (as yourself and root , for example). See the online documentation on bash for more information on customizing
prompts, including many more options you can use to display system
information in your prompt.
Another shell variable that is extremely important during
interactive use is
PATH
, which
contains a list of all the directories that hold commands or other
programs you are likely to execute. A default path is set up for you
when bash starts. You may wish to modify the
default to add other directories that hold programs you need to
run.
Note
Every file in the Linux filesystem can be specified in terms
of its location. The less program, for example,
is located in the directory /usr/bin . Placing /usr/bin in your
PATH
enables you to execute less by simply typing
less
rather than the explicit
/usr/bin/less
.
Also be aware that " . " (the current
directory) is not included in the
PATH
either implicitly (as it is in DOS ) or explicitly for security reasons. To
execute a program named foo in the current
directory, simply run
./foo
.
For bash to find and execute the command
you enter at the prompt, the command must be one of the
following:
A
bash
built-in command that is part of bash itself
An executable program located in a directory listed in the
PATH
variable
An executable program whose filename you specify
explicitly
The shell holds
PATH
and
other variables for its own use. However, many of the shell’s
variables are needed during the execution of
Leslie A. Kelly
Verna Clay
Tim McLoughlin
Stephanie Johnson
Roberto Buonaccorsi
Kelly Gendron
JS Taylor
Sally Armstrong
Holly Webb
Lynsay Sands