topics that matter; ideas worth sharing

share a tip, submit a link, add something new

Traveling down the Unix $PATH

March 15, 2001, 04:39 PM —  Unix Insider — 




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:

  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

I like it!
Post a comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
Resources
White Paper

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.

Webcast

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.

White Paper

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.

Free stuff
Featured Sponsor

Get a broad understanding of important regulations and how you can make sure your site is in adherence.





Learn how VeriSign SGC-enabled SSL Certificates can help improve site security and customer confidence in the free white paper, "How to Offer the Strongest SSL Encryption." In this paper you will learn the differences between weak and strong encryption and what they mean for your site's performance.

Get VeriSign's free white paper: "The Latest Advancements in SSL Technology" and learn about the benefits of strong SSL encryption, Extended Validation (EV) SSL and security trust marks and what these SSL offerings can do for your site.

Now with Extended Validation (EV) SSL available from VeriSign, you can show your customers that they can trust your site. Learn about EV SSL benefits in this free VeriSign white paper.

More Resources