Quote arguments.
Avoid calling the same command twice.
Directly test the exit status of the command. (@zendeavor)
Use the POSIX `command -v` function instead of type/which. (@havenwood)