Bash Execution order: Aliases, Functions, Built-ins, and Path Notes
In what order do bash command get precedence?
First the aliases get executed if the shell is the interactive one. If the command name contains no slashes, the shell attempts to locate it. If there exists a shell function by that name, that function is invoked. If the name
does not match a function, the shell searches for it in the list of shell builtins. If a match is found, that builtin is invoked.
If the name is neither a shell function nor a builtin, and contains no slashes, bash searches each element of the path for a directory containing an executable file by that name. Bash uses a hash table to remember the full pathnames of executable files. A full search of the directories in path is performed only if the command is not found in the hash table. If the search is unsuccessful, the shell searches for a defined shell function named command_not_found_handle. If that function exists, it is invoked in a separate execution environment with the original command and the original command’s arguments as its arguments, and the function’s exit status becomes the exit status of that subshell. If that function is not defined, the shell prints an error message and returns an exit status of 127.
If the search is successful, or if the command name contains one or more slashes, the shell executes the named program in a separate execution environment.
If this execution fails because the file is not in executable format, and the file is not a directory, it is assumed to be a shell script and the shell executes it.