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)