25

I'm installing a set of softwares/packages/formulas via homebrew with command like

brew install <formula1>
...
brew cask install <formula2>
...

I wonder if it's a good idea to verify if the softwares <formula1>, ..., <formula2>, ... are already installed and only trigger the above commands for the ones NOT already installed. If so, how?

47

you could do something like this:

brew list <formula1> || brew install <formula1>

This will error on list and continue with install if not installed otherwise it will just list package files. (one could modify this further as a function or alias in .bashrc to make it easier to type)

3
  • 1
    How can this be done in a Brewfile? – Jack Moody Jan 28 '19 at 16:04
  • 1
    @JackMoody It sounds like what you want to do is add it as a dependency in the brew file using depends_on – svangordon Oct 2 '20 at 1:52
  • can confirm. brew list python@3.9 &>/dev/null || brew install python@3.9; works. – airtonix Feb 4 at 0:28
15

It should also be noted that you can type brew info <formula> which will tell you whether or not a formula is installed. You can parse the response for "Not installed" and then run the installer if it finds the string.

4
  • the disadvantage of brew info <formula> is that it loads very slowly, as it has to fetch the statistics of the formula from the Internet. If you use brew list <formula> | grep "No such keg" (or something similar), the result will be displayed much faster. – Raptor Oct 13 '20 at 14:38
  • brew info always has a exit code 0. so it's useless. brew list python@3.9 &>/dev/null || brew install python@3.9; works because it returns 1 when there's nothing found. – airtonix Feb 4 at 0:30
  • @airtonix Just because it always has an exit code of 0 doesn't make it useless. As I mention in my post this method requires that you parse the response. – Josh Correia Feb 4 at 17:05
  • @JoshCorreia nah it's useless. you want to know it failed or not. this is why exit codes exist. – airtonix Feb 27 at 3:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .