Recently one of my bash scripts failed because cmake and zip / unzip were not installed on a system.

What would be a convenient way to check for installed packages in $PATH env?

I would like to check $PATH directly for cmake and the like since my script is running on Debian, Ubuntu, Arch, and so on. Thus, I would prefer to not use a package manager, because I would have to implement it several times using dpkg, pacman, ... based on the distribution the script is currently executed on.

  • If you're just looking for tools like cmake and zip then those are installed by the package manager and the binaries will be in /usr/bin unless you built them from source and put them elsewhere. If you installed them via the package manager then you can just use the package tool like rpm or dpkg-query to search for the installed packages. – Nasir Riley Mar 9 at 23:47
  • @NasirRiley yes, but the problem is that my script is running on different distributions. Hence, I would like to avoid implementing that with rpm, dpkg, pacman, ... It is quite cumbersome to maintain this by hand so I would prefer to not use different package managers. – daniel451 Mar 10 at 0:11

As mentioned in "Why not use "which"? What to use then?", the most portable way of testing whether a command is found in $PATH or not is through:

if command -v given-command > /dev/null 2>&1; then
  echo given-command is available
  echo given-command is not available

If your script is for public consumption, you should obviously document the prerequisites so that another person is able to install the dependencies before running the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.