Detect if homebrew package is installed Detect if homebrew package is installed bash bash

Detect if homebrew package is installed


You can use

brew ls --versions myformula

to output the installed versions of the respective formula. If the formula is not installed, the output will be empty.

When using a recent versions of homebrew, which you can get with brew update, you can just run this (thanks Slaven):

if brew ls --versions myformula > /dev/null; then  # The package is installedelse  # The package is not installedfi

That said, it is probably a good idea to check for the existence of the tool at all and not just checking for the respective homebrew package (e.g. by searching for the executable in the $PATH). People tend to install tools in a rather large amount of ways in practice, with homebrew being just one of them.


What about?

for pkg in macvim ngrep other needed packages; do    if brew list -1 | grep -q "^${pkg}\$"; then        echo "Package '$pkg' is installed"    else        echo "Package '$pkg' is not installed"    fidone


# install if we haven't installed any versionbrew ls --versions $lib || brew install $lib# install if we haven't installed latest versionbrew outdated $lib || brew install $lib