5

In bash one can define functions and aliases, and bash has its own built-ins. Additionally, bash can run executables available on the system.

For the sake of this question, assume that the shell (whether interactive or non-interactive) has alias expansion enabled.

If a user-defined function has the same name as an executable, will bash run the function or the executable?

If a user-defined function has the same name as a bash built-in, will bash run the function or the built-in?

And so on - what is the order of precedence for what bash will run if there are two (or more) things with the same identifier?

4
  • 1
    This answer lists the precedence of aliases, functions, built-ins, ... as well as ways to influence the selection process.
    – Socowi
    Commented Jul 24, 2021 at 19:28
  • Simply use: type -all command_name - That shows the order. - First the alias than function and rest like ordered in ${PATH} Commented Jul 24, 2021 at 19:41
  • @koyaanisqatsi Huh, I didn't know you could use -all with type. It's undocumented.
    – wjandrea
    Commented Jul 24, 2021 at 19:51
  • On my os i found this defined in .bashrc as: alias where='type -all' Commented Jul 25, 2021 at 7:59

2 Answers 2

7

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:

  1. If the command name contains slashes, the shell will try to execute the path indicated by the command name
  2. If a shell function exists with the provided name, the shell function is invoked
  3. If a shell built-in exists with the provided name, the shell built-in is invoked
  4. 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)
  5. 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
  6. If the search is unsuccessful, bash will invoke the command_not_found_handle function, if it is defined
  7. 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.

1
  • Not only the first word of a simple command. If the alias definition ends in a whitespace, the word following the alias name is also subjected to alias expansion. If the alias definition is one of {, (, if, or while, again, the word that follows is alias expanded. Commented Jul 24, 2021 at 18:00
2

What order does bash use to decide whether to use an alias, function, or executable?

Alias is not "executed". If aliases are enabled and when an alias matches then the word is replaced. Always.

Then, function first, executable second, if ordering these two.

Aliases are only expanded in interactive shells (not in scripts)

In bash, not really, just interactive shells have aliases enabled by default. You can enable aliases in non-interactive shells and disable in interactive shell - it's a separate property.

If a user-defined function has the same name as an executable, will bash run the function or the executable?

Function.

If a user-defined function has the same name as a bash built-in, will bash run the function or the built-in?

Function.

what is the order of precedence for what bash will run if there are two (or more) things with the same identifier?

The behavior is documented in bash documentation command search and execution. The behavior is standardized in POSIX Shell command language, section Command search and execution.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.