Java Reference
In-Depth Information
The most important shell variable to know is
PATH
. It defines the directo-
ries in the filesystem where the shell will look for programs to execute. When
you type a command like
ls
or
javac
the shell will look in all of the directories
specified in the
PATH
variable, in the order specified, until it finds the
executable.
$ echo $PATH
/usr/local/bin:/usr/bin:/usr/X11R6/bin:/bin:.
$
The
PATH
shown in the example has five directories, separated by colons
(“
:
”). (Note the fifth one, the “
.
”; it says to look in the current directory.)
Where do you suppose it will find
cat
? You can look for it yourself by search-
ing in each directory specified in
PATH
. Or you can use the
which
command:
$ which cat
/bin/cat
$
Some commands (like
exit
) don't show up, since they are built into the
shell. Others may be aliases—but that opens a whole other topic that we aren't
covering here. Just remember that each directory in the
PATH
variable is exam-
ined for the executable you want to run. If you get a
command not found
error,
the command may be there, it just may not be on your
PATH
.
To look at it the other way around: If you want to install a command so
that you can execute it from the command line, you can either always type its
full pathname, or (a more user-friendly choice) you can set your
PATH
variable
to include the location of the new command's executable.
So where and how do you set
PATH
? Whenever a shell is started up, it reads
some initialization files. These are shell scripts that are read and executed as if
they were typed by the user—that is, not in a subshell. Among other actions,
they often set values for variables like
PATH
. If you are using
bash
, look at
.bashrc
in your home directory.
Shell scripts are just shell commands stored in a file so that you don't need
to type the same commands and options over and over. There are two ways to
run a shell script. The easiest, often used when testing the script, is
$ sh myscript