Command Execution

If a command name is present after the previous steps, the shell executes the given command.

If the command name does not include slashes (‘/’),

  1. If the command name matches a special built-in utility, the special built-in utility is executed; otherwise,

  2. If the command name matches a shell function name, that function is executed; otherwise,

  3. If the command name matches a regular built-in utility, that built-in utility is executed; otherwise,

  4. The shell searches each path in the PATH environment variable for an executable of the same name, executing the first match found.

If the command name includes at least one slash (‘/’), the shell treats it as a path to an executable and attempts to execute it directly without performing any kind of lookup.

Built-in Utilities

Built-in utilities are utilities that the shell implements internally–rather than by spawning a separate, standalone process, the shell itself performs the behavior of the given utility. There are certain utilities that must be built-in because they perform tasks that modify the shell process. For example, the cd (Change Directory) utility changes the shell’s current working directory, something a child process would not be able to do, so it is implemented as a built-in.

Some built-in utilities are designated as special built-in utilities, which gives them the highest precedence in command search and execution, so that functions and other system utilities with the same names cannot override them. Additionally, syntax errors with special built-ins may cause the shell itself to abort–not normally possible with regular commands. Finally, variable assignments performed as part of a special built-in command remain in effect after the special built-in command completes, rather than affecting the execution of only that command as described on the previous page.

These are:

  • break

  • : (colon)

  • continue

  • . (dot)

  • eval

  • exit

  • export

  • readonly

  • return

  • set

  • shift

  • times

  • trap

  • unset

Others are designated as regular built-in utilities, which have a lower precedence than functions during command search and execution, so that they may be overriden by user-defined functions. Syntax errors with regular built-ins do not cause the shell to abort. The standard regular built-ins are:

  • alias

  • bg

  • cd

  • command

  • false

  • fc

  • fg

  • getopts

  • jobs

  • kill

  • newgrp

  • pwd

  • read

  • true

  • umask

  • unalias

  • wait

Additionally, any implementation of the shell may choose to implement other utilities as regular built-ins. For example, most shell implementations provide the true and false utilities as regular built-ins. This is done because it is more efficient to simply change the shell’s status variable to a 0 or a 1, rather than spawn an entire process and capture its exit status as would normally be required.