The shell has a number of commands that are built-in. This means that the shell itself executes the command, instead of running an external program in a separate process. Furthermore, POSIX distinguishes between “special” built-ins and “regular” built-ins. [Most regular built-ins] have to be built-in for the shell to function correctly (e.g.,
read). Others are typically built into the shell only for efficiency (e.g.,trueandfalse). The standard allows other commands to be built-in for efficiency as well, but all regular built-ins must be accessible as separate programs that can be executed directly by other binary programs. The distinction between special and regular built-in commands comes into play when the shell searches for commands to execute. The command-search order is special built-ins first, then shell functions, then regular built-ins, and finally external commands found by searching the directories listed in$PATH. This search order makes it possible to define shell functions that extend or override regular shell builtins. This feature is used most often in interactive shells. For example, suppose you would like the shell's prompt to contain the last component of the current directory's pathname. The easiest way to make this happen is to have the shell changePS1each time you change directories. You could just write your own [cd] function [for this]. There is one small fly in the ointment here. How does the shell function access the functionality of the "real"cdcommand?...What's needed is an "escape hatch" that tells the shell to bypas the search for functions and access the real command. This is the job of thecommandbuilt-in command.[However] the
builtincommandcommand is not a special builtin command! Woe be to the shell programmer who defines a function named command! The POSIX standard provides the following two additional special qualities for the special built-in commands:
- A syntax error in a special built-in utility may cause a shell executing that utility to abort, while a syntax error in a regular built-in utility shall not cause a shell executing that utility to abort. If a special built-in utility encountering a syntax error does not abort the shell, its exit value shall be nonzero.
- Variable assignments specified with special built-in utilities remain in effect after the built-in completes; this shall not be the case with a regular built-in or other utility. [That is] you can specify variable assignment at the front of a command and the variable will have that value in the environment of the executed command only, without affecting the variable in the current shell or subsequent commands. (e.g.
PATH=/bin:/usr/bin: awk '...') However, when such an assignment is used with a special built-in command, the assignment stays in effect from then on, even after the special built-in completes.
Arnold Robbins and Nelson H. F. Beebe. Classic Shell Scripting: Hidden Commands that Unlock the Power of Unix (p. 262-5). O'Reilly Media. Kindle Edition.
Note that the command command causes the shell to treat the specified command and arguments as a simple command, suppressing shell function lookup. From the IBM Docs
Normally, when a / (slash) does not precede a command (indicating a specific path), the shell locates a command by searching the following categories:
special shell built-ins shell functions regular shell built-ins PATH environment variable For example, if there is a function with the same name as a regular built-in, the system uses the function. The command command allows you to call a command that has the same name as a function and get the simple command.
The command -v and command -V commands write to standard output what path name will be used by the shell and how the shell interprets the command type (built-in, function, alias, and so forth). Since the -v and -V flags produce output in relation to the current shell environment, the command command is provided as a Korn shell or POSIX shell regular built-in command. The /usr/bin/command command might not produce correct results, because it is called in a subshell or separate command execution environment,. In the following example the shell is unable to identify aliases, subroutines, or special shell commands:
(PATH=foo command -v) nohup command -v
Thus, in my previous example, I used the bash builtin instead of command because had I put it in a subshell, it would not have worked properly.