Lookup order inside bash
When bash reads a simple command, it checks the name against a series of tables in order. In an interactive shell, aliases come first. Next come functions, then builtins, and then the program search along the directories in the PATH variable. Reserved words such as if and while are recognized earlier, when the parser reads the command. A name can exist in several tables, and the first match wins, which is why a function named ls changes the behavior of ls in your shell without changing the program on disk.
The search through PATH is cached in a hash table. The first time you run a program, bash remembers its full path. If you later install a new version in an earlier directory, the old path stays cached until you run hash -r or start a new shell.
command -v: the name the shell would run
The builtin command -v prints how a name would be interpreted. For an external program it prints the full path. For a builtin, a keyword, an alias, or a function, it prints the bare name or the definition. Its exit status is zero when the name is found, which makes it the right tool for an if test in a script. Use it in place of which, which may be missing on a minimal system.
cd /workspace
command -v printf
command -v ls
command -v no-such-command; echo "status: $?"The first command prints printf, because it is a builtin. The second prints the path of the ls program. The third prints nothing and reports a status of one.
type: the kind and the detail
The builtin type tells you more. It names the kind of each word, such as builtin, keyword, alias, function, or file. The option -t prints only the kind, which is convenient in a script. The option -a lists every match in lookup order, so you can see that a name exists as a builtin and as a program at the same time. The shell echo command is the classic example, and it is one of the most useful things to check when output looks odd.
type -t cd
type -a echoThe first command prints builtin. The second prints a line saying echo is a shell builtin and a line with the path of the external echo program, which shows that both exist.
which: useful, but not always present
The external program which searches the PATH and prints the first match. It does not know about aliases, functions, or builtins, so it can be misleading. It is also not part of the POSIX standard, and some minimal systems do not install it. For scripts, command -v is more reliable. For a quick human check on a system that has which, it is fine, provided you remember what it does not report.
The lab does not include man pages. When you want to know what a builtin does, use help name, such as help cd. For external programs, use the --help option.
Finding files, not commands
To locate a file by name rather than a command, use find. The command find / -name passwd is slow and noisy on a real system, so restrict the starting point and the type. A search of the PATH for a program is quicker with command -v. The two tools answer different questions: command -v asks what the shell will run, while find asks where a file with a given name exists.
Debugging a wrong program
When a command behaves unexpectedly, run type -a on its name. If you see an alias or a function that you forgot, the cause is in your shell configuration. If you see two programs, the first one in PATH is the one that runs. A common fix is to reorder PATH in the shell profile, and a second fix is to call the program by its full path, which bypasses every table.
Hash tables and command caching
The cache of program paths is a practical source of confusion. When you install a new version of a program in a directory that comes earlier in PATH, bash keeps using the old path until the cache is cleared. The builtin hash lists the cached paths, and hash -r clears them. Starting a new shell does the same thing. When a program seems to run an old version after an update, check hash before you change anything else.
The cache is only for external programs. Aliases, functions, and builtins are never cached in this way, because the shell looks them up each time from its own tables. This is why a function that replaces a program takes effect at once, while an updated program on disk may need a cache clear.
Asking the shell about a name in a script
A script that needs a tool should check for it before it uses it. The pattern command -v tool >/dev/null 2>&1 || { echo "tool is required" >&2; exit 1; } fails early with a clear message. A check that uses type is also valid, but command -v is the form designed for tests, because it prints only the name or path and returns a clean status.
A quick way to see the whole lookup at once is to give the name to type with the -a option and read the list from top to bottom. The first line is the one that runs. If the first line is an alias, the program on PATH is ignored in your interactive shell, but a script that starts bash without your profile will still find the program. That difference explains many bugs that appear only in cron jobs or only in one terminal.
Practice in the lab
The lab uses command -v and type to answer three questions about names: whether printf is a builtin, whether cd is a builtin, and where echo lives. Each answer goes into a file so the checker can compare the text.