How would I validate that a program exists, in a way that will either return an error and exit, or continue with the script?
It seems like it should be easy, but it’s been stumping me. …
if ! command -v <the_command> &> /dev/null then echo "<the_command> could not be found" exit fi
which. Not only is it an external process you’re launching for doing very little (meaning builtins like
command are way cheaper), you can also rely on the builtins to actually do what you want, while the effects of external commands can easily vary from system to system.
- Many operating systems have a
whichthat doesn’t even set an exit status, meaning the
if which foowon’t even work there and will always report that
fooexists, even if it doesn’t (note that some POSIX shells appear to do this for
- Many operating systems make
whichdo custom and evil stuff like change the output or even hook into the package manager.