Recently I found command: command
which has no manual entry but help displays as follows:
$ help command
command: command [-pVv] command [arg ...]
Execute a simple command or display information about commands.
Runs COMMAND with ARGS suppressing shell function lookup, or display
information about the specified COMMANDs. Can be used to invoke commands
on disk when a function with the same name exists.
Options:
-p use a default value for PATH that is guaranteed to find all of
the standard utilities
-v print a description of COMMAND similar to the `type' builtin
-V print a more verbose description of each COMMAND
Exit Status:
Returns exit status of COMMAND, or failure if COMMAND is not found.
Is command -v
is alternative of which
?
What arguments are accepted by this command and how/when to use command
?
command
is a bash builtin as we can see:So we know
command
is provided by our shell, bash. Digging intoman bash
we can see what its use is:(from
man bash
):Essentially you would use
command
to bypass "normal function lookup". For example, say you had a function in your.bashrc
:Normally, when you run
say_hello
in your terminal bash would find the function namedsay_hello
in your.bashrc
before it found, say, an application namedsay_hello
. Using:makes bash bypass its normal function lookup and go straight to either builtins or your
$PATH
. Note that this function lookup also include aliases. Usingcommand
will bypass both functions and aliases.If the
-p
option is provided bash bypasses your custom$PATH
and uses its own default.The
-v
or-V
flags bash prints a description (short for-v
, long for-V
) of the command.Note: As souravc pointed out in the comments an easier method for finding information about shell builtins can be found here: How to make `man` work for shell builtin commands and keywords?
This is built-in command of the Bash shell.
The only advantage I see with this built-in is summarized in the following sentence of the help text:
So if you want to execute a program (a binary file saved somewhere on your disk), and an internal shell function of the same name exists, then you can invoke your program using this built-in.
And yes,
command -v
will give the same kind of result astype
.I've found it also under the Dash shell.
It has two different uses:
One use is to ignore aliases and functions, and run the executable file found in PATH, even when an alias or a function with the same name exists.
As example, I'll use an alias for
ls
that appends a/
to directory names:In an interactive shell, it may be more convenient to use a backslash before the command name as alternative, shorter syntax:
The other use is to find the command that will be run when the commands name isn't used by using the option
-v
. It seems to be the most portable/POSIX variant ofwhich
.command
is useful, for example, if you want to check for the existence of a particular command.which
includes aliases into the lookup so it is not suitable for this purpose because you don't want a random alias to be considered as the command in question.In other words, you can have a small function in a shell script like this:
And then test for an available command (here,
dialog
), like so:It lets you run a shell command ignoring any shell functions.
http://ss64.com/bash/command.html
I've seen in used as a technique to pass command line arguments to executables that need to be ran as a background process. Here's an example found online