Five kinds of name
A command name can refer to one of five things. A reserved word is a keyword such as if, for, or {, and it is recognized only in command position. An alias is a short name for a piece of text that the shell substitutes before parsing, in interactive shells. A shell function is a named group of commands defined inside the shell. A builtin is a command that is implemented inside the shell itself, such as cd, export, or printf. A program is an executable file found through $PATH, such as /bin/ls.
When a plain word is not a keyword, bash checks these in order. First it checks for an alias, which is expanded when the line is read in an interactive shell. Next it checks for a function with that name. Then it checks for a builtin. If none of those match, it searches the directories listed in $PATH from left to right and runs the first executable file it finds. Bash remembers those lookups in a hash table, so repeated runs do not search the path again. The hash builtin shows the table, and hash -r clears it.
Why cd, export, and read are builtins
A program started by bash runs in a child process. A child receives a copy of the parent environment, but it cannot change the parent state. The working directory, the shell variables, and the exported environment all belong to the shell process. If cd were an external program, it would change the directory of a child and then exit, and your prompt would not move. The same is true of export and read. They must act on the shell that runs them, so bash implements them as builtins.
You can see the child-process rule with a short experiment. The first command runs a new bash that changes its own directory. The second command runs in your shell and prints the directory that your shell still has.
bash -c 'cd /tmp'
pwdThe pwd output is still /workspace, the directory you were in before. The child shell moved to /tmp and then exited, taking its change with it. A builtin avoids this, because it runs inside your shell and changes the state directly.
Asking bash what a name is
The type builtin tells you what bash will run for a name. It prints the kind of name and, for a program, its path. The -a option lists every match in lookup order, and -t prints a single word that is easy to test in a script. The output for type is the most reliable answer to the question "what runs when I type this", so prefer it over which, which is an external program that only searches $PATH and cannot see aliases, functions, or builtins.
type printf
type time
type [
type -a echoThe first command reports that printf is a shell builtin, and time is a shell keyword. The bracket [ is a builtin, while [[ is a keyword with its own grammar. The last command prints two lines: echo is a shell builtin, followed by echo is /bin/echo. Bash prefers the builtin, and the program is still available if you call its full path.
The lab does not have man pages installed. For a builtin, use help name, for example help cd. For a program, use --help, and for system calls and the rest of the reference, use the online man pages at man7.org. This is a common situation on minimal containers, so it is worth getting used to.
type -t prints one of alias, keyword, function, builtin, or file. The command -v form prints the path for a program, or the bare name for a builtin, keyword, alias, or function, and it is meant for use in if tests.
Bypassing functions and aliases
Sometimes you need the original command even though a function or alias has taken its name. The command builtin runs a name while skipping aliases and functions, and it finds builtins and programs. The builtin builtin runs only a shell builtin, and fails if there is none with that name. Putting a backslash in front of a word stops alias expansion for that word. Quoting is the simplest way to say "not the alias".
The next block defines a function named echo. After that, an ordinary echo runs the function, but command echo skips it and runs the builtin. The final unset removes the function again.
echo() { printf 'function: %s\n' "$*"; }
echo hi
command echo hi
unset -f echoThe first call prints function: hi, and the second prints hi. Bash does not check functions when you use command, so the builtin runs. This is the normal way to call a wrapper from inside the wrapper itself without recursing forever.
Aliases and functions with the same name
Because aliases are checked before functions, an alias wins when both exist with the same name. The example defines a function first and then an alias with the same name. Typing the name runs the alias, and the backslash form runs the function. Aliases are expanded only in interactive shells, so this behavior is most visible at the prompt, and scripts do not normally see your aliases.
greet() { echo function; }
alias greet='echo alias'
greet
\greet
unalias greet
unset -f greetThe first greet prints alias, because the alias text echo alias replaces the word before lookup. The backslash version prints function, because quoting prevented alias expansion and the function was found next. Removing both definitions returns the names to normal.
Hands-on: check the lookup order
Run the commands below, in order. Each one answers a question about the names you use, and together they show the lookup order from the first section of this unit.
type -a echo
type -t cd
command -v printf
command -v lsThe type -a echo output lists the builtin before the program, which is the order bash uses. type -t cd prints builtin. The command -v printf line prints the bare name, because printf is a builtin, while command -v ls prints the path of the ls file found through $PATH.