Traveling down the Unix $PATH

Unix Insider –

Why is it that some commands can simply be executed, while others must be
<font face="Courier">./executed</font>
? In other words, why do some commands need a dot-slash in front of them to run? Rather than giving you a short answer, I am going to explore a couple of things and hope you find them enlightening.

If you create a new shell script, will you be able to run it with the first command below, or will you need to resort to the second?

<font face="Courier">
$ newscript 
$ ./newscript 
$ 
</font>

Commands in Unix are either builtins or executables. Builtins are part of the shell you are currently running. Examples include

<font face="Courier">echo</font>
,
<font face="Courier">read</font>
, and
<font face="Courier">export</font>
.

Any command that is not built in must be an executable. There are two types of executables: shell script languages, such as

<font face="Courier">sh</font>
,
<font face="Courier">ksh</font>
,
<font face="Courier">csh</font>
, or
<font face="Courier">perl</font>
, or compiled executables, such as a program written in C and compiled down to a binary.

Commands created by using an alias in

<font face="Courier">ksh</font>
also break down into these two main categories, because the command is translated and then issued as either a builtin or an executable. The following examples create aliases for the builtin
<font face="Courier">echo</font>
and the executable
<font face="Courier">grep</font>
.

The shell can always locate builtins, because they are built in to the currently executing shell.

<font face="Courier">
$ alias sayit='echo ' 
$ alias g='grep ' 
</font>

In each case, after alias substitution is completed, the command becomes a builtin or an executable.

You can use the

<font face="Courier">type</font>
command to verify the nature of
<font face="Courier">echo</font>
.

<font face="Courier">
$ type echo 
echo is a shell builtin 
$ 
</font>

Now use the

<font face="Courier">type</font>
command to check on
<font face="Courier">grep</font>
.
<font face="Courier">type</font>
will give you the directory that contains the executable
<font face="Courier">grep</font>
program.

<font face="Courier">
$ type grep 
grep is /bin/grep 
$ 
</font>

Whether you enter

<font face="Courier">grep</font>
as a command or ask for its location using
<font face="Courier">type</font>
, the operating system finds
<font face="Courier">grep</font>
by using the
<font face="Courier">$PATH</font>
environment variable. If
<font face="Courier">type</font>
can find
<font face="Courier">grep</font>
, then echoing out the
<font face="Courier">$PATH</font>
variable will verify that the path to the directory containing
<font face="Courier">grep</font>
is part of the
<font face="Courier">$PATH</font>
variable.

<font face="Courier">
$ type grep 
grep is /bin/grep 
$ echo $PATH 
/bin:/usr/bin:/usr/local/bin:/home/mjb/bin 
$ 
</font>

The directories listed in

<font face="Courier">$PATH</font>
are separated by colons. The above example includes
<font face="Courier">/bin</font>
,
<font face="Courier">/usr/bin</font>
,
<font face="Courier">/usr/local/bin</font>
, and
<font face="Courier">/home/mjb/bin</font>
. As an aside, the
<font face="Courier">type</font>
command is probably a builtin.

<font face="Courier">
$ type type 
type is a shell builtin 
$ 
</font>

Another useful command similar to

<font face="Courier">type</font>
is
<font face="Courier">whereis</font>
, which will usually locate a command and its manual entry.

<font face="Courier">
$ whereis grep 
grep: /bin/grep /usr/man/man1/grep.1 
$ 
</font>

The shell reads and interprets strings of characters and words typed at the keyboard. Unix shells operate in a simple loop:

  1. Accept a command
  2. Interpret the command
  3. Execute the command
  4. Wait for another command

In step 3, the shell searches for the command to be executed first in the shell itself and then in each of the directories listed in the

<font face="Courier">$PATH</font>
. If it can't be found in one of these path directories, an error results.

<font face="Courier">
$ zowie 
zowie: command not found 
$ 
</font>

It's important to note that the shell does not search the current directory unless that directory happens to be in the

<font face="Courier">$PATH</font>
variable. This is important to understand, especially if you came to Unix from an MS-DOS background. MS-DOS uses a
<font face="Courier">PATH</font>
variable as well, but it searches the user's the current directory before it searches in any directories in the user's
<font face="Courier">PATH</font>
.

Some users have had the foresight to include the current directory in their

<font face="Courier">$PATH</font>
variable. This will appear as a single dot, the Unix shorthand for current directory. Note the dot at the end of the
<font face="Courier">$PATH</font>
variable below.

<font face="Courier">
$ echo $PATH 
/bin:/usr/bin:/usr/local/bin:/home/mjb/bin:. 
</font>

If you have the dot in your

<font face="Courier">$PATH</font>
variable, create a new directory under your home directory, such as
<font face="Courier">$HOME/temp</font>
, and change to it.

<font face="Courier">
$ cd $HOME 
$ mkdir temp 
$cd temp 
$ 
</font>

Use the

<font face="Courier">vi</font>
editor to create a simple script.

<font face="Courier">
# sayhello 
echo "Hello" 
</font>

Save it and change the mode to executable.

<font face="Courier">
$ chmod a+x sayhello 
$ 
</font>

If you have the dot in your

<font face="Courier">$PATH</font>
variable, you'll be able to execute the command directly.

<font face="Courier">
$ sayhello 
Hello 
$ 
</font>

If you don't have a dot in your

<font face="Courier">$PATH</font>
variable, the computer will search through your
<font face="Courier">$PATH</font>
(anywhere but the current directory) and report failure.

<font face="Courier">
$ sayhello 
sayhello: command not found 
$ 
</font>

If you type an unadorned command such as

<font face="Courier">sayhello</font>
, the computer searches for it. However, if you apply any additional path information to the command, the shell assumes that you are giving an absolute path and only looks where you tell it to. Consequently,
<font face="Courier">./sayhello</font>
locates the command in the current directory.

<font face="Courier">
$ ./sayhello 
Hello 
$ 
</font>

Obviously, the dot-slash version works whether or not you have a dot in your

<font face="Courier">$PATH</font>
variable, because the dot-slash precludes the shell's search for the command.

To get a dot into your

<font face="Courier">$PATH</font>
if you don't have one, you need to edit your personal startup profile, usually called
<font face="Courier">.profile</font>
and located in your
<font face="Courier">$HOME</font>
directory. Look for a line that exports the
<font face="Courier">PATH</font>
variable, such as line 5 below. (Line numbers are included here for easy reference, but are not part of the file.) This file already has a line 4 that includes some local additions to the default
<font face="Courier">$PATH</font>
.

<font face="Courier">
1.  # .profile 
2.  # User specified environment 
3.  USERNAME="mjb" 
4.  PATH=$PATH:$HOME/bin 
5.  export USERNAME PATH 
</font>

If line 4 did not exist, you'd want to create a line that read:

<font face="Courier">
PATH=$PATH:. 
</font>

In this case, edit line 4 to read:

<font face="Courier">
PATH=$PATH:$HOME/bin:. 
</font>

Now, whenever you log in, the dot is added to your search

<font face="Courier">$PATH</font>
for commands.

So, the simple rule is: if you want to execute any command in any directory not on your

<font face="Courier">$PATH</font>
, including the current directory, you must specify a path to locate the command. This includes a
<font face="Courier">./</font>
for the current directory.

Top 10 Hot Internet of Things Startups
Join the discussion
Be the first to comment on this article. Our Commenting Policies