It is best to avoid using which. As an external tool, it does not necessarily exist. There will be differences between releases. The which command of some systems will not set a valid exit status, and there is some uncertainty. sex.
Bash provides some built-in commands such as hash, type, and command that can also meet the requirements.
$ 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; }
It is best to avoid using which. As an external tool, it does not necessarily exist. There will be differences between releases. The which command of some systems will not set a valid exit status, and there is some uncertainty. sex.
Bash provides some built-in commands such as hash, type, and command that can also meet the requirements.
See http://stackoverflow.com/questions/59...
for detailswhich
When the command cannot be found, "xxx not found" will be output to stderr.