open.itworld.com
  Search  
Security Home Page Security Webcasts Security White Papers Security Newsletters Security News Open Topics Careers ITworld Voices ITwhirled The Security site of ITworld.com

Unix Tip: Shells and search order

ITworld.com 1/22/2007

Sandra Henry-Stocker, ITworld.com

Send in your Unix questions today! | See additional Unix tips and tricks

It's easy to take the Unix command line for granted. That is, it's easy to get used to typing in commands and getting responses without thinking very much about how the system determines what command to run. Some of the commands we type are binary files that are part of the OS, some are scripts, some are shell built-ins and some are aliases that are configured into our accounts. Still others may be shell routines. The order that Unix systems use in evaluating the commands we enter is not solely dependent on our PATH variables. Instead, the search order follows a predefined order. If you happen to have a script that has the same name as a Unix command, an alias and a shell routine or built-in, for example, how do you know which one you will end up executing when you press the return key?

One way to determine the order in which the commands we use will be evaluated is to create a script, alias and shell routine that have the same names as a system command or shell built-in and see what happens. Let's try that with the date command.

First, let's create a script:

#!/bin/ksh
# date


echo At the beep, the time will be
echo ^G
echo now

To be sure this script has a chance to run, I will give it precedence over the system command by putting the current location temporarily in my search path:

export PATH=.:$PATH

Next, I'll create an (ksh) alias:

alias date="echo The time is now"

Last, I'll create a Korn shell routine:

date () {
echo "The first day of the rest of your life"
}

When I type "date" on the command line, the response shows clearly that I am running the alias:

$ date
The time is now

To determine what comes in second place, I need to undefine my alias and try the date command again:

$ unalias date
$ date
The first day of the rest of your life

This time, my function is executed.

To remove the function, I use the unset command. This leaves me running my date script, as long as I remain in the current directory, and the system date command otherwise.

$ unset -f date
$ date
At the beep, the time will be
^G
now


When I rename my date script, the system date command is executed:

$ date
Thu Jan 31 21:41:51 EST 2007

To determine where shell built-ins fall into the search order, I can compare the search order of built-ins with aliases and scripts in much the same way.

First, a set script:

#!/bin/ksh

echo Set a spell

Then a set alias:

alias set="echo matching items"

When I type "set", my alias is executed. When I remove the alias, the shell built-in is run. This tells me that built-ins have a lower precedence than aliases, just as functions do.

The shell won't permit you to create a function with the same name as a built-in. When it sees the name of a built-in, it expects the syntax of the built-in to follow and will reject the syntax required to create a function. That said, there's no way to test the precedence of functions versus shell built-in commands in this way. I am told, however, that functions are examined first. This leaves the overall order in which the shell searches for what to run when you type a command to this:

alias
function
shell built-in
script or command (depending on your search path)

On this topic

 

Sandra Henry-Stocker has been administering Unix systems for more than 18 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She currently works for TeleCommunication Systems, a wireless communications company, in Annapolis, Maryland, where no one else necessarily shares any of her opinions. She lives with her second family on a small farm on Maryland's Eastern Shore. Send comments and suggestions to bugfarm@gmail.com.




Sponsored Links

Sign up for a Microsoft Dynamics® CRM WEBCAST
Hear globally recognized leaders in customer strategy discuss the importance and evolution of CRM.
Sun Microsystems' - FREE 60 DAY TRIAL OFFER!
Test Sun's Newest Servers BEFORE YOU BUY. Plug Them In With Access To Full Technical Support.
100% Web Based Help Desk Software
Easy to use, customizable to meet your needs, powerful and scalable. Free online demo. Try it today!
Sign up for a Microsoft Dynamics® CRM WEBCAST
Hear globally recognized leaders in customer strategy discuss the importance and evolution of CRM.
Used and Refurbished HP ProCurve Switches
Lifetime Warranties, Professional Testing & Shipping on all HP Equipment Purchases!
» Buy a link now

Advertisements
Sponsored links
Locate Hidden Software on business PCs with this free tool
Bring harmony to your mix of UNIX-Linux-Windows computing environments
Top 5 Reasons to Combine App Performance and Security
KODAK i1400 Series Scanners stand up to the challenge
 Home   Open source  Operating systems  Unix
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   IDG Connect   IDG World Expo   Infoworld   ITworld   JavaWorld   LinuxWorld  MacUser   Macworld   Network World   PC World   Playlist  

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.