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:
|
|