Friday, May 3, 2024
84
rated 0 times [  84] [ 0]  / answers: 1 / hits: 36920  / 2 Years ago, sun, august 7, 2022, 12:35:34

The command command 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.

Questions



  • Can command -v be used as alternative to which?

  • What is the purpose and usecase of command?


More From » command-line

 Answers
4

command is a bash builtin as we can see:



seth@host:~$ type command
command is a shell builtin


So we know command is provided by our shell, bash. Digging into man bash we can see what its use is:



(from man bash):



command [-pVv] command [arg ...]
Run command with args suppressing the normal shell function
lookup. Only builtin commands or commands found in the PATH are
executed. If the -p option is given, the search for command is
performed using a default value for PATH that is guaranteed to
find all of the standard utilities. If either the -V or -v
option is supplied, a description of command is printed. The -v
option causes a single word indicating the command or file name
used to invoke command to be displayed; the -V option produces a
more verbose description. If the -V or -v option is supplied,
the exit status is 0 if command was found, and 1 if not. If
neither option is supplied and an error occurred or command
cannot be found, the exit status is 127. Otherwise, the exit
status of the command builtin is the exit status of command.


Essentially you would use command to bypass "normal function lookup". For example, say you had a function in your .bashrc:



function say_hello() {
echo 'Hello!'
}


Normally, when you run say_hello in your terminal bash would find the function named say_hello in your .bashrc before it found, say, an application named say_hello. Using:



command say_hello  


makes bash bypass its normal function lookup and go straight to either builtins or your $PATH. Note that this function lookup also include aliases. Using command 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?


[#23592] Monday, August 8, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
fulpu

Total Points: 116
Total Questions: 118
Total Answers: 104

Location: Solomon Islands
Member since Fri, Oct 8, 2021
3 Years ago
;