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 installed
else
# The package is not installed
fi
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.
brew --cellar "$formula" >/dev/null 2>&1
--cellar formula: Display the location in the cellar where formula would be installed, without any sort of versioned directory as the last path.
brew man page; would have loved to give it as an answer – 166_MMXif [ ! -x "$(command -v PKG_EXEC)" ]; then # package not installed fi
– JBallin