78

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?
Semnodime
  • 103
  • 4
Pandya
  • 34,843
  • 42
  • 126
  • 186

6 Answers6

86

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?

Seth
  • 57,282
  • 43
  • 144
  • 200
  • 1
    Also look at [thsi](http://manpages.ubuntu.com/manpages/trusty/en/man1/command.1posix.html) – sourav c. Oct 19 '14 at 03:48
  • 3
    try `sudo apt-get install manpages-posix`. It is not installed by default. look [here](http://askubuntu.com/a/439752/127327) – sourav c. Oct 19 '14 at 03:53
  • I'm new at Linux, so i'm still learning about commands. I got the idea about the "command" command (just was scared, because I thought it could cause some damage in my system). Just to know, is that thing "shell" important to learn? – Nori-chan Oct 19 '14 at 04:03
  • 1
    @Nori-chan [Bash](http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29) is Ubuntu's default [*shell*](http://en.wikipedia.org/wiki/Shell_%28computing%29). It is the thing that interprets the commands you enter and decides what to do and how to do it with the information you enter. By learning the Un*x command-line you are (in a way) learning bash :) – Seth Oct 19 '14 at 04:06
  • Just to know, since I just used the command "command", so actually, nothing happened right?. Also, @Seth, thank you for editing my post (making it more accurate) and also answering me. – Nori-chan Oct 19 '14 at 04:18
  • @Nori-chan Yes, since you ran `command` with no arguments it simply exited silently, nothing really happened. Thank *you* for asking a great question! – Seth Oct 19 '14 at 04:19
  • You can use `help command` to get usage info on a builtin rather than searching through the `man` page for it. – LawrenceC Oct 19 '14 at 16:06
  • +1 for a great pedagocical way to answer. However, I'd suggest to say that `command something` bypasses both function AND alias named `something`, whereas `\something` would just bypass the alias `something` but would execute the function. To my knowledge, there is no way to just bypass a function without bypassing also an alias named the same way. (Your answer says "it bypass the function (...) and go straignt to either builtins or your path" but doesn't mention explicitely it bypasses also aliases, which the OP (or maybe another person) may still need to see explictely written to "grok" it) – Olivier Dulac Oct 20 '14 at 11:22
  • @OlivierDulac Good point, I hadn't thought of that. The manpage doesn't explicitly say it, but I tested and you are correct. I will update the answer. – Seth Oct 21 '14 at 01:51
  • `vim /usr/bin/command` gives ```builtin echo ${0##*/} | tr \[:upper:] \[:lower:]` ${1+"$@"}```. This is also the same for `vim /usr/bin/alias`. I wonder what that means? – Ben Butterworth Nov 10 '20 at 20:39
  • Key takeaway: _"Using `command` will bypass both functions and aliases."_ – Gabriel Staples Jul 27 '21 at 22:48
16

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:

Can be used to invoke commands on disk when a function with the same name exists.

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 as type.

I've found it also under the Dash shell.

Benoit
  • 7,497
  • 1
  • 24
  • 34
  • 2
    What's worth adding more explicitly is that although `command (name)` ignoes shell functions, `command -v (name)` does not. `command -v (name) >/dev/null` is supposed to be the portable way of checking whether the command with that name exists, regardless of whether it's a shell builtin, function, or external utility. – hvd Aug 18 '14 at 11:49
  • 1
    command -v is the posix alternative to which, type, etc, http://stackoverflow.com/questions/762631/find-out-if-a-command-exists-on-posix-system – Javier López Aug 18 '14 at 13:23
  • For a real-world example, the Android AOSP build environment (at some point after v4.2.2) defines a shell function named 'make' that will spit out information about whether or not a build succeeds and how long it took. Other shell functions in the AOSP build environment use `command make` to invoke the actual-factual make program. Unfortunately, I have other shell scripts that broke when the AOSP environment started adding shit to what the `make` program outputs and it was irritating as hell figuring out where that extra output was mysteriously coming from. – Michael Burr Mar 20 '17 at 23:20
12

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:

$ alias ls='ls --classify'
$ ls -d .
./
$ command ls -d .
.

In an interactive shell, it may be more convenient to use a backslash before the command name as alternative, shorter syntax:

$ \ls -d .
.

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 of which.

$ command -v ls
alias ls='ls --classify'
$ command -v sed
/bin/sed
Kaz Wolfe
  • 33,802
  • 20
  • 111
  • 168
Volker Siegel
  • 12,820
  • 5
  • 48
  • 65
  • Definitely my favorite explaination here. Prevents a mucked up command, or worse, if an alias already has options assigned. Aside eg \execmd to bypass alias of eg execmd. Arrived here after seeing command used in alias section of a server's .bashrc file - where you definitely would want bad alias. Bit of funny head scratcher than command command (Bash built-in ) gets used in alias to bypass aliases. – Cymatical Jun 27 '21 at 22:35
  • Re: "_It seems to be the most portable/POSIX variant of which._" — For me (and in 2022) while `command` behaves as described, `which` isn't aware of aliases. – stafusa Apr 05 '22 at 17:23
4

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:

exists() {
  command -v "$1" >/dev/null 2>&1
}

And then test for an available command (here, dialog), like so:

if ! exists dialog ; then
   echo "This script requires 'dialog'."
   echo "Install it with 'sudo apt-get install dialog', then try again!"
   exit 1
fi
pa4080
  • 29,351
  • 10
  • 85
  • 161
Ville
  • 141
  • 3
3

It lets you run a shell command ignoring any shell functions.

http://ss64.com/bash/command.html

Paul Tanzini
  • 3,807
  • 1
  • 11
  • 19
0

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

command ping -i 5 google.com &

https://www.digitalocean.com/community/tutorials/how-to-use-bash-s-job-control-to-manage-foreground-and-background-processes#starting-processes

jxramos
  • 101
  • 2
  • I'm not entirely sure if this is suggested as necessary to get arguments fed to the command without the `&` character being interpreted as another argument to the main command. – jxramos Jun 07 '22 at 02:12
  • 2
    There's no need to use `command` here unless you have a function named `ping` too. Having `command` there makes no difference to the parsing of `&`. – muru Jun 07 '22 at 03:10