How to Check if a Program Exists From a Bash/Shell Script


You can look to use:

command -v <the_command>

There are also 2 other ways, that we will run through a little further down, but for now..

How to use the command if a conditional

if ! command -v <the_command> &> /dev/null
then
    echo "<the_command> could not be found"
    exit
fi

To summarize, there are 3 ways you can do this

Option 1 - Using command

command -v foo >/dev/null 2>&1 || { echo >&2 "foo is not installed. Aborting."; exit 1; }

Option 2 - Using type

type foo >/dev/null 2>&1 || { echo >&2 "foo is not installed. Aborting."; exit 1; }

Option 3 - Using hash

hash foo 2>/dev/null || { echo >&2 "foo is not installed. Aborting."; exit 1; }