You can use the command
builtin, which is POSIX compatible:
if [ -x "$(command -v "$cmd")" ]; then
echo "$cmd is in \$PATH"
fi
The executable check is needed because command -v
detects functions and aliases as well as executables.
In Bash, you can also use type
with the -P
option, which forces a PATH
search:
if type -P "$cmd" &>/dev/null; then
echo "$cmd is in \$PATH"
fi
As already mentioned in the comments, avoid which
as it requires launching an external process and might give you incorrect output in some cases.