ITworld.com
  Search  
ITworld Home Page ITworld Webcasts ITworld White Papers ITworld Newsletters ITworld News ITworld Topics Careers ITworld Voices ITwhirled Changing the way you view IT

Traveling down the Unix $PATH

Unix Insider 9/15/00

unixinsiderhome
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.

On this topic

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 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.




Sponsored Links

Workflow Enabled Help Desk & IT Service Management
Automate service desk activities and integrate processes across IT. Learn more here.
IP Networks Boost Secure Health Communications
AT&T provides secure communication to keep health care moving forward.
CAPTURE Quad-Core Performance
Check Out The Latest In Capturing The Value Of Xeon® Quad-Core Servers For Your Business.
TOSHIBA SATELLITE PRO Notebook – Save With Synnex!
SYNNEX RESELLERS - Great Deals On Toshiba. Business Computing Has Never Been More Affordable!
Metadata Management Software
MetaCenter: Plug & play metadata management software for enterprise systems. Features: data dictionary, process documentation, impact analysis, search across multiple systems, web-based interface, reports, dashboards, import, export and more!
» Buy a link now

Advertisements
Sponsored links
Top 5 Reasons to Combine App Performance and Security
KODAK i1400 Series Scanners stand up to the challenge
Locate Hidden Software on business PCs with this free tool
Bring harmony to your mix of UNIX-Linux-Windows computing environments
 Home   Application Development  Systems and network programming  Shell Programming
www.itworld.com    open.itworld.com     security.itworld.com     smallbusiness.itworld.com
storage.itworld.com     utilitycomputing.itworld.com     wireless.itworld.com

 
Contact Us   About Us   Privacy Policy    Terms of Service   Reprints  

CIO   Computerworld   CSO   GamePro   Games.net   Industry Standard   Infoworld   ITworld  
JavaWorld   LinuxWorld  MacUser   Macworld   Network World   PC World   Playlist  

DEMO   IDG Connect   IDG Knowledge Hub   IDG TechNetwork   IDG World Expo  

Copyright © Computerworld, Inc. All rights reserved

Reproduction in whole or in part in any form or medium without express written permission of Computerworld Inc. is prohibited. Computerworld and Computerworld.com and the respective logos are trademarks of International Data Group Inc.