1.4.1.7.
Executing commands
Up one level
-
When executing a command, the words that the parser has marked as variable assignments (preceding the command name) and redirections are saved for later reference. Words that are not variable assignments or redirections are expanded; the first remaining word after expansion is taken to be the name of the command and the rest are arguments to that command. Then redirections are performed, then strings assigned to variables are expanded. If no command name results, variables will affect the current shell environment.
An important part of the tasks of the shell is to search for commands. Bash does this as follows:
-
Check whether the command contains slashes. If not, first check with the function list to see if it contains a command by the name we are looking for.
-
If command is not a function, check for it in the built-in list.
-
If command is neither a function nor a built-in, look for it analyzing the directories listed in PATH. Bash uses a hash table (data storage area in memory) to remember the full path names of executables so extensive PATH searches can be avoided.
-
If the search is unsuccessful, bash prints an error message and returns an exit status of 127.
-
If the search was successful or if the command contains slashes, the shell executes the command in a separate execution environment.
-
If execution fails because the file is not executable and not a directory, it is assumed to be a shell script.
-
If the command was not begun asynchronously, the shell waits for the command to complete and collects its exit status.

