5

I would like to use Homebrew to install GNU versions of several utilities, and use the GNU versions by default with their usual names.

From the output of brew install gnu-tar and by reading How to replace Mac OS X utilities with GNU core utilities? I learned that I can add /usr/local/opt/gnu-tar/libexec/gnubin to my $PATH variable. However, for other packages, the gnubin directory is under a different path, like /usr/local/opt/coreutils/libexec/gnubin for the coreutils package.

Do I have to add each of these to my path by hand, or does Homebrew provide an easy way to collect all of the entries in various gnubin directories into one place?

I suppose I could put some kind of auto-finding script in my .bash_profile like:

GNUBINS=$(find -L /usr/local/opt -type d -name gnubin | tr ' ' ':')
export PATH=$GNUBINS:$PATH

However this feels a little clunky and insecure, and I was hoping for a Homebrew-specific solution.

3 Answers 3

6

The proposed find based solution takes about 5 seconds to execute on my system. The following bash snippet executes in a fraction of a second.

if type brew &>/dev/null; then
  HOMEBREW_PREFIX=$(brew --prefix)
  # gnubin; gnuman
  for d in ${HOMEBREW_PREFIX}/opt/*/libexec/gnubin; do export PATH=$d:$PATH; done
  # I actually like that man grep gives the BSD grep man page
  #for d in ${HOMEBREW_PREFIX}/opt/*/libexec/gnuman; do export MANPATH=$d:$MANPATH; done
fi
1
  • Nice and fast! One caveat: if there are no such gnubins it will put literally /usr/local/opt/*/libexec/gnubin on the path, but presumably one is only running this if one has at least one gnubin there. :) (and furthermore, my original find command suffers from a similar issue)
    – amacleod
    Oct 9, 2019 at 13:58
1

You don't have homebrew/bin directory?

I am using Boxen (including Homebrew) at work, Homebrew is installed to /opt/boxen/homebrew.

I have /opt/boxen/homebrew/bin directory with links to all the Homebrew utilities.

coreutils is a bunch of commands, not a single formula, so I think you still need to add it to your PATH but you should not have to do this for other GNU software like gnu-sed, gawk or findutils if you install them with --default-names option.

Here is a simple list of my /opt/boxen/homebrew/bin

[admin@nzx-mb-125:/opt/boxen/homebrew/bin] : ls -l | grep -i awk
lrwxr-xr-x  1 admin  staff   28 16 Dec 16:51 awk -> ../Cellar/gawk/4.1.0/bin/awk
lrwxr-xr-x  1 admin  staff   29 16 Dec 16:51 gawk -> ../Cellar/gawk/4.1.0/bin/gawk
lrwxr-xr-x  1 admin  staff   35 16 Dec 16:51 gawk-4.1.0 -> ../Cellar/gawk/4.1.0/bin/gawk-4.1.0
lrwxr-xr-x  1 admin  staff   30 16 Dec 16:51 igawk -> ../Cellar/gawk/4.1.0/bin/igawk
[admin@nzx-mb-125:/opt/boxen/homebrew/bin] : ls -l | grep -i sed
lrwxr-xr-x  1 admin  staff   31 16 Dec 16:52 sed -> ../Cellar/gnu-sed/4.2.2/bin/sed
2
  • thanks for the formatting tips, I will keep that in mind in future. Dec 17, 2013 at 22:26
  • I do not appear to have homebrew/bin. I guess that would be specific to Boxen. If I find that I need to link a lot of GNU binaries, I'll consider giving Boxen a try.
    – amacleod
    Dec 18, 2013 at 21:45
1

No. I don't think there's any shortcuts provided by Homebrew. You have to prepend the directory into PATH manually.

function gnuon {
  local gnu_packages=(
    coreutils
    findutils
    grep
    gnu-sed
    gnu-tar
  )
  local i
  for i in ${gnu_packages[@]}; do
    if [[ -d "/usr/local/opt/${i}" ]]; then
      export PATH="/usr/local/opt/${i}/libexec/gnubin:$PATH"
      export MANPATH="/usr/local/opt/${i}/libexec/gnuman:$MANPATH"
    fi
  done
}
gunon

You must log in to answer this question.

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