This may seem like an advanced topic, but it is critical to understand how BASH decides which command to execute. The common assumption is that the PATH variable determines the command to be executed. In fact, this is the last thing BASH checks. This is true for all Linux shells. The search order for a command is as follows:
- If the command name contains a slash, then BASH follows the path to the command. For example,
$ /bin/pwd
tells BASH that path to the command. - BASH then checks to see if there is an alias for the command. We will learn how to define aliases later in this course. For now, you can just type
$ alias
to get a list of command aliases. If there is an alias, BASH substitutes the command name with the alias, and goes back to step 1. - BASH is an interactive programming environment. Functions provide a mechanism for performing repeated tasks by invoking the function with a list of parameters. The following command displays the list of functions defined in your environment:
$ compgen -A function - Since BASH is a programming language, it has certain keywords used for programming. Since keywords act like commands, BASH checks to see if the "command" is really a "keyword." The following example checks the keyword if:
$ type -a ifif is a shell keyword - BASH then checks to see if the command is built into the shell (referred to as builtin commands). For commonly used commands, a builtin command saves the time of searching through the PATH variable. Other builtin commands are unique to BASH. The following example shows that the type command, itself, is a builtin command:
$ type -a typetype is a shell builtin - Finally, BASH searches the path variable for a command. The following example shows how to use the which command to find out the path to a command:
$ which cat/bin/cat
There are duplicate names in the system. For example,
$ type -a pwd pwd is a shell builtin pwd is /bin/pwd
Why are there two versions of the same command? The Bourne Shell had a very limited number of builtin commands, which did not include commands such as pwd. For improved performance, BASH made it a builtin command. Since there are many scripts that invoke the Bourne Shell, the /bin/pwd command has to remain. The problem is that the options are different. This gets even more exciting when there are aliases with the same name as the command. While the following may produce different results when executed with different distros, the dir command is an example of a command that is an alias and a command:
$ type -a dir dir is aliased to `ls -l' dir is /usr/bin/dir dir is /usr/bin/X11/dir
If you had used the which command, you would have only gotten the following answer:
$ which dir /usr/bin/dir
While all this seems a bit confusing, it can save a lot of frustration when you are searching for documentation on running a command. It also helps to the search order when the command does not produce the expected results. All it takes is an alias, or a function, with the same name as a command.




Recent comments