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.
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.
In case you want to check if a program exists and is really a program, not a bash built-in command, then
command
,type
andhash
are not appropriate for testing as they all return 0 exit status for built-in commands.For example, there is the time program which offers more features than the time built-in command. To check if the program exists, I would suggest using
which
as in the following example:The hash-variant has one pitfall: On the command line you can for example type in
to have process executed. For this the parent folder of one_folder must be in $PATH. But when you try to hash this command, it will always succeed:
It will tell according to the location if the program exist or not
hash foo 2>/dev/null
: works with zsh, bash, dash and ash.type -p foo
: it appears to work with zsh, bash and ash (busybox), but not dash (it interprets-p
as an argument).command -v foo
: works with zsh, bash, dash, but not ash (busybox) (-ash: command: not found
).Also note that
builtin
is not available withash
anddash
.Try using:
or
From the bash manpage under Conditional Expressions: