bash: Command Search and Execution
1
1 3.7.2 Command Search and Execution
1 ----------------------------------
1
1 After a command has been split into words, if it results in a simple
1 command and an optional list of arguments, the following actions are
1 taken.
1
1 1. If the command name contains no slashes, the shell attempts to
1 locate it. If there exists a shell function by that name, that
1 function is invoked as described in ⇒Shell Functions.
1
1 2. If the name does not match a function, the shell searches for it in
1 the list of shell builtins. If a match is found, that builtin is
1 invoked.
1
1 3. If the name is neither a shell function nor a builtin, and contains
1 no slashes, Bash searches each element of '$PATH' for a directory
1 containing an executable file by that name. Bash uses a hash table
1 to remember the full pathnames of executable files to avoid
11 multiple 'PATH' searches (see the description of 'hash' in ⇒
Bourne Shell Builtins). A full search of the directories in
1 '$PATH' is performed only if the command is not found in the hash
1 table. If the search is unsuccessful, the shell searches for a
1 defined shell function named 'command_not_found_handle'. If that
1 function exists, it is invoked with the original command and the
1 original command's arguments as its arguments, and the function's
1 exit status becomes the exit status of the shell. If that function
1 is not defined, the shell prints an error message and returns an
1 exit status of 127.
1
1 4. If the search is successful, or if the command name contains one or
1 more slashes, the shell executes the named program in a separate
1 execution environment. Argument 0 is set to the name given, and
1 the remaining arguments to the command are set to the arguments
1 supplied, if any.
1
1 5. If this execution fails because the file is not in executable
1 format, and the file is not a directory, it is assumed to be a
11 SHELL SCRIPT and the shell executes it as described in ⇒Shell
Scripts.
1
1 6. If the command was not begun asynchronously, the shell waits for
1 the command to complete and collects its exit status.
1