Find brew installation location on OS X

I am creating a BASH script that requires installing several applications. ffmpeg and sox

To make sure they are installed when my script is executed, I first check the installation of Homebrew with

 #!/bin/bash which -s brew if [[ $? != 0 ]] ; then # Install Homebrew /usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)" fi 

Then I verify that sox and ffmpeg installed with:

 echo "---- checking for sox ----" which -s sox || /usr/local/bin/brew install sox echo "---- checking for ffmpeg ----" which -s ffmpeg || /usr/local/bin/brew install ffmpeg 

The problem that I encountered is when Homebrew is installed, but is in a non-standard location.

I need to use the full path to Homebrew because this script is executed in Playtypus .

So the question is: how can I reliably set the installed Homebrew path in a BASH script?

+6
source share
1 answer

Answering my own question ...

You can check the output of which brew and deal with things accordingly. To gracefully deal with the case where Homebrew is not installed, you can use if which brew 2> /dev/null , which redirects stderr to /dev/null .

brew --prefix also useful here, as it gives the path to where the applications installed on Homebrew are installed, are symbolically linked, not their actual installation path.

A script that works and shows this work:

 #!/bin/bash if which brew 2> /dev/null; then brewLocation=`which brew` appLocation=`brew --prefix` echo "Homebrew is installed in $brewLocation" echo "Homebrew apps are run from $appLocation" else echo "Can't find Homebrew" echo "To install it open a Terminal window and type :" echo /usr/bin/ruby -e \"\$\(curl\ \-fsSL\ https\:\/\/raw\.github\.com\/Homebrew\/homebrew\/go\/install\)\" fi 

Thanks to Allendar for pointers.

+9
source

All Articles