Find the install location of brew on OS X

16,562

Solution 1

Answering my own question...

You can test 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 is also useful here as it give the path to where Homebrew installed applications are symlinked to, rather than their actual install path.

A script which works and shows this working :

#!/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 the pointers.

Solution 2

Just to add to this, Homebrew's --prefix mode has been enhanced here in the far-flung future of 2020 (or maybe it was always this way), so that it now takes a package name as an argument. Meaning locating those "keg-only" packages which aren't linked into standard paths is as easy as:

$ brew --prefix ffmpeg
/usr/local/opt/ffmpeg
Share:
16,562
dwkns
Author by

dwkns

A UX'er getting back into dev after a long absence.

Updated on June 14, 2022

Comments

  • dwkns
    dwkns almost 2 years

    I'm creating a BASH scrip which requires a couple of applications to be installed. ffmpeg and sox

    To ensure they are in place when my script runs I first check for 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 check that sox and ffmpeg are 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 I am facing is when Homebrew is installed but in a non-standard location.

    I have to use the full path to Homebrew because this script is being run within Playtypus.

    So the question is : How can I reliably get the installed path of Homebrew in a BASH script?