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?
As hinted in the introduction to this article, another method of finding what Homebrew packages are installed on a Mac by simply using the ls command to show where Homebrew packages are installed: The output of that command will be every package installed through Homebrew, as they always end up in that directory by default.
Homebrew will update its list of packages and then download and install the tree command: Homebrew installs files to /usr/local by default, so they won’t interfere with future macOS updates. Verify that tree is installed by displaying the command’s location with the which command:
Homebrew includes a simple and convenient command to list all packages that have been installed through brew, the syntax is as follows: Sample output may look something like the following, depending on what packages and their dependencies you have installed: You may have fewer or more brew packages installed, depending on your particular setup.
You can now install Homebrew. To install Homebrew, you’ll download an installation script and then execute the script. First, download the script to your local machine by typing the following command in your Terminal window:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With