Check if a Program Exists From Your Bash Script
Published: Apr 12, 2019
Updated: May 3, 2021
Updated: May 3, 2021
Lately I’ve had the Bash bug… and I’m continually impressed at what can be accomplished with this tool. Just the other day, I needed to check for the existence of some programs before doing the rest of my scripting work.
Originally I was doing this with which
, but after reading this detailed stackoverflow answer, I’m now using the more portable command -v
.
Code #
PROGRAM="foo"
if ! command -v ${PROGRAM} >/dev/null; then
echo "This script requires ${PROGRAM} to be installed and on your PATH ..."
exit 1
fi
Output #
$ ./script.sh
This script requires foo to be installed and on your PATH ...
$ echo ${?}
1