command.md

Question: How to check if a program exists from a Bash script?

Answer: three possible method:

  • POSIX compatible:

    1
    
    $ command -v <the_command>
    
  • For bash specific environments:

    1
    
    $ hash <the_command> # For regular commands. Or...
    
    1
    
    $ type <the_command> # To check built-ins and keywords
    

Many operating systems have a which that doesn’t even set an exit status, meaning the if which foo won’t even work there and will always report that foo exists, even if it doesn’t (note that some POSIX shells appear to do this for hash too).

So, don’t use which. Instead use one of these:

1
2
3
4
5
$ command -v foo >/dev/null 2>&1 || { echo >&2 "I require foo but it's not installed.  Aborting."; exit 1; }

$ type foo >/dev/null 2>&1 || { echo >&2 "I require foo but it's not installed.  Aborting."; exit 1; }

$ hash foo 2>/dev/null || { echo >&2 "I require foo but it's not installed.  Aborting."; exit 1; }