Project16.Find Unix Commands


Project 16. Find Unix Commands

"When I type ls, where is the executable found, and will it be overridden by a Bash built-in command or function of the same name?"

Unix is susceptible to confusion over different entities with the same name. Unix commands, built-in shell commands, and user-defined functions and aliases, for example, might have a common name. This project helps you understand and control the shell's use of these same-name functions and commands, whether you're calling them interactively or writing scripts that use them.

Use Type to Identify a Command

Suppose you wish to discover in which directory a particular Unix command lives. In a script, for example, you might want to call the command by its full pathname to avoid relying on the PATH variable (see "The PATH" later in this project). Alternatively, you may wish to see which instance of a command will be executed in the case where several exist. Bash's built-in command type tells you this. Here, we discover that mkdir is to be found in the directory /bin.

$ type mkdir mkdir is /bin/mkdir


The name type doesn't make sense until you realize what type actually does. It reports on how Bash will interpret a command in terms of its type, the type being one of the following:

  • An alias

  • A keyword like if or for

  • A function

  • A shell built-in command

  • A Unix executable command

The list order corresponds to Bash's search order. When a same-name command exists in many forms, an alias form will override a built-in, and a built-in will override a Unix executable. Here are some examples.

$ type pushd pushd is a shell builtin $ type cd cd is aliased to `cd '


(This assumes you have defined such an alias.)

Use option -a to reveal all possible types that a command currently has. The types are listed in the order in which Bash considers them. In this example, the cd command will be interpreted as an alias. If the alias is deleted, the built-in command will be used.

$ type -a cd cd is aliased to `cd ' cd is a shell builtin


Override Bash's Order

Three Bash built-in commands are available to override the natural order in which commands are considered. They are particularly useful when employed in functions and scripts.

  • The command command forces a command to be considered as either a built-in or a Unix executable. If the command is an alias or function too, the alias or function will be ignored, even though aliases and functions are normally considered first.

  • The builtin command forces a command to be considered as a built-in only. No other types will be considered.

  • The enable command enables and disables built-in commands. See the example of using enable below.

Here's an example using command. We define a function called ls to be shorthand for ls -al argument-list. The following attempt will not work because it gets stuck in a loop: Function ls attempts to call the Unix command ls, but because Bash considers function names ahead of Unix commands, the function ends up calling itself instead. Futile attempts to call command ls continue foreveror until we interrupt the process by pressing Control-c.

$ ls () { ls -al $*; } $ ls ^C


Can One Disable enable?

And how is it enabled again? This is presented purely as fun. Something for you to think about:

$ enable -n enable $ enable enable enable: Operation failed: clienterror-not-found $ builtin enable enable -bash: builtin: enable: not a shell builtin $


We can no longer get at the built-in enable.


Our second attempt works. When we employ the command command, function ls is made to call the Unix command ls instead of itself.

$ ls () { command ls -al $*; } $ ls total 560 drwxr-xr-x 47 saruman saruman 1598 19 May 19:04 . drwxrwxr-t 18 root admin 612 9 Apr 18:22 .. -rwxr-xr-x 1 saruman saruman 15364 18 May 16:52 .DS_Store ...


Here's an example of using the enable command. We see that the command pwd is both a Bash built-in command and a Unix executable by typing

$ type -a pwd pwd is a shell builtin pwd is /bin/pwd


Tip

A quick way to override an alias and run the original command is to precede it with a backslash (\) symbol.

$ \cd



Let's disable the Bash built-in version by using command enable and specifying option -n to disable.

$ enable -n pwd $ type -a pwd pwd is /bin/pwd


Now we restore the built-in version by typing

$ enable pwd $ type -a pwd pwd is a shell builtin pwd is /bin/pwd


The PATH

When searching for a Unix command, type checks all directories listed in the PATH environment variable in the order listed. You can view the path by typing

$ echo $PATH /bin:/sbin:/usr/bin:/usr/sbin


Tip

Run a script or command that resides in the current directory by typing

$ ./script-command-name



An executable in any directory outside the path is invisible to type and therefore to Bash too. Bash will not run such a command if you simply type its name; you must use the command's full pathname to invoke it.

Learn More

Project 50 shows how to add directories to the search path.


If a command of the same name exists in several directories listed in PATH, Bash will invoke the first one it encounters. To invoke a different variant, specify a full pathname. We might have a version of ls in /usr/local/bin as well as the more usual location of /bin, and we may have /usr/local/bin earlier in our PATH. We use type with option -a to identify all versions, which will list them in the order in which Bash considers them.

$ type -a ls ls is /usr/local/bin/ls ls is /bin/ls


The Tcsh Shell

The Tcsh shell does not have command type but instead uses a builtin command called which.

% which ls ls:      aliased to ls --color=tty % which mkdir /bin/mkdir % which which which: shell built-in command.


Tcsh allows alias commands to be overridden temporarily in favor of Unix commands by escaping them with a backslash character, exactly as Bash does. In fact, which recognizes this too.

% which \ls /bin/ls


Note

Bash rehashes automatically and does not need, or have, a rehash command.


If you're using the Tcsh built-in command which, be aware that there's a Unix command of the same name.

% which \which /usr/bin/which


The Unix which command should not be used. It searches only the standard path and misses out on aliases. It's written for use with the csh shell.

When Tcsh starts up, it searches the directories listed in PATH, building a cache of command names. If you install a new command, which and the Tcsh shell ordinarily can't find it until you begin a new shell session. Remedy this problem by typing the command rehash to tell the current shell to rebuild its command-name cache.




Mac OS X UNIX 101 Byte-Sized Projects
Mac OS X Unix 101 Byte-Sized Projects
ISBN: 0321374118
EAN: 2147483647
Year: 2003
Pages: 153
Authors: Adrian Mayo

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net