Traveling down the Unix $PATH
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
Symantec Backup Exec 12 and Backup Exec System Recovery 8 deliver industry leading Windows data protection and system recovery. Download this whitepaper to find out the top reasons to upgrade and how to get continuous data protection and complete system recovery.
Data and system loss — from a hard drive failure, malicious attack, natural disaster, or simple human error — can happen anytime. Don’t leave your business vulnerable. Make sure you have a secure recovery strategy in place. Symantec's latest backup and system recovery technology can efficiently restore critical applications, individual emails and documents and even restore your entire system in minutes in the event of a loss.
Businesses face a growing challenge to ensure that the IT environment is properly protected. Backup Exec 12 integrates with other applications in the Symantec family of products, to complement your current data protection strategy, keep your data securely backed up and make it recoverable when you need it most.







