Traveling down the Unix $PATH
Why are some commands executed, and some ./executed?
Summary
Why do some commands need a dot-slash in order to run? In this month's Unix 101 column, Mo Budlong explores the answer to this question, and explains the difference between built-in and executable commands. (1,200 words)
This article is based on a question that came out of July's installment of Unix 101:
Why is it that some commands can simply be executed, while others must be ./executed ? 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?
$ newscript
$ ./newscript
$
Commands in Unix are either builtins or executables. Builtins are part of the shell you are currently running. Examples include echo , read , and export .
Any command that is not built in must be an executable. There are two types of executables: shell script languages, such as sh , ksh , csh , or perl , or compiled executables, such as a program written in C and compiled down to a binary.
Commands created by using an alias in ksh 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 echo and the executable grep .
The shell can always locate builtins, because they are built in to the currently executing shell.
$ alias sayit='echo '
$ alias g='grep '
In each case, after alias substitution is completed, the command becomes a builtin or an executable.
You can use the type command to verify the nature of echo .
$ type echo
echo is a shell builtin
$
Now use the type command to check on grep . type will give you the directory that contains the executable grep program.
$ type grep
grep is /bin/grep
$
Whether you enter grep as a command or ask for its location using type , the operating system finds grep by using the $PATH environment variable. If type can find grep , then echoing out the $PATH variable will verify that the path to the directory containing grep is part of the $PATH variable.
$ type grep
grep is /bin/grep
$ echo $PATH
/bin:/usr/bin:/usr/local/bin:/home/mjb/bin
$
The directories listed in $PATH are separated by colons. The above example includes /bin , /usr/bin , /usr/local/bin , and /home/mjb/bin . As an aside, the type command is probably a builtin.
$ type type
type is a shell builtin
$
Another useful command similar to type is whereis , which will usually locate a command and its manual entry.
$ whereis grep
grep: /bin/grep /usr/man/man1/grep.1
$
The shell reads and interprets strings of characters and words typed at the keyboard. Unix shells operate in a simple loop:
- Accept a command
- Interpret the command
- Execute the command
- 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 $PATH . If it can't be found in one of these path directories, an error results.
$ zowie
zowie: command not found
$
It's important to note that the shell does not search the current directory unless that directory happens to be in the $PATH variable. This is important to understand, especially if you came to Unix from an MS-DOS background. MS-DOS uses a PATH variable as well, but it searches the user's the current directory before it searches in any directories in the user's PATH .
Some users have had the foresight to include the current directory in their $PATH variable. This will appear as a single dot, the Unix shorthand for current directory. Note the dot at the end of the $PATH variable below.
$ echo $PATH
/bin:/usr/bin:/usr/local/bin:/home/mjb/bin:.
If you have the dot in your $PATH variable, create a new directory under your home directory, such as $HOME/temp , and change to it.
$ cd $HOME
$ mkdir temp
$cd temp
$
Use the vi editor to create a simple script.
# sayhello
echo "Hello"
Save it and change the mode to executable.
$ chmod a+x sayhello
$
If you have the dot in your $PATH variable, you'll be able to execute the command directly.
$ sayhello
Hello
$
If you don't have a dot in your $PATH variable, the computer will search through your $PATH (anywhere but the current directory) and report failure.
$ sayhello
sayhello: command not found
$
If you type an unadorned command such as sayhello , 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, ./sayhello locates the command in the current directory.
$ ./sayhello
Hello
$
Obviously, the dot-slash version works whether or not you have a dot in your $PATH variable, because the dot-slash precludes the shell's search for the command.
To get a dot into your $PATH if you don't have one, you need to edit your personal startup profile, usually called .profile and located in your $HOME directory. Look for a line that exports the PATH 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 $PATH .
1. # .profile
2. # User specified environment
3. USERNAME="mjb"
4. PATH=$PATH:$HOME/bin
5. export USERNAME PATH
If line 4 did not exist, you'd want to create a line that read:
PATH=$PATH:.
In this case, edit line 4 to read:
PATH=$PATH:$HOME/bin:.
Now, whenever you log in, the dot is added to your search $PATH for commands.
So, the simple rule is: if you want to execute any command in any directory not on your $PATH , including the current directory, you must specify a path to locate the command. This includes a ./ for the current directory.
Contact
us for a free consultation. |