Precedence
First, only if the shell is in interactive mode or the expand_aliases
shell option is set using shopt
, the first word of a simple command will be substituted by a matching alias if the matching alias exists.1
Next, bash will check if an identifier is defined as one of the following in the order below, and execute the command once the first match is found:
- If the command name contains slashes, the shell will try to execute the path indicated by the command name
- If a shell function exists with the provided name, the shell function is invoked
- If a shell built-in exists with the provided name, the shell built-in is invoked
- If the command name is in bash's hash table of remembered commands, bash will attempt to execute the path in the hash table (if the path is no longer valid, bash will not try to search its
PATH
)
- If the command name is not in bash's hash table, bash will search for an executable matching the name in its
PATH
, searching the directories in its PATH
in order, and executing the first such executable it finds
- If the search is unsuccessful, bash will invoke the
command_not_found_handle
function, if it is defined
- Else, bash prints an error message and returns an exit status of 127
Controlling what bash uses
To force bash to use a built-in instead of expanding an alias or invoking a function of the same name, use the builtin
built-in.
To force bash to use a built-in or an executable found in the PATH
instead of using an alias or function, use the command
built-in.
To force bash to use an executable file on-disk, either use the path to the executable if known, or use type -P <executable_name>
to get the path and then use that path to invoke the executable.
To delete an alias, use unalias
. To delete a function, use unset
.
To delete an entry from bash's hash table, use hash -d <name>
. To see the path stored for an entry in bash's hash table, use hash -t <name>
. (This can be helpful to debug when the hash table is storing an old path that no longer points to the desired executable.)
More info/documentation
For more information, see the bash man page (especially the COMMAND EXECUTION section), or the GNU bash manual.
1It is a minor oversimplification to say that interactive bash shells always expand aliases, as alias expansion can be disabled in interactive shells with shopt
. It is just that the default is for interactive bash shells to expand aliases.
type -all command_name
- That shows the order. - First the alias than function and rest like ordered in${PATH}
-all
withtype
. It's undocumented..bashrc
as:alias where='type -all'