Link

GNU tools on macOS with homebrew

Table of contents

  1. Goal
  2. Install homebrew
  3. Install GNU command line tools
  4. Updating $MANPATH

Goal

If you’re one of those persons which spends most of his time working on Linux environments from a MacBook, you’ve may found out tools like grep, cut or sed do not behave the same way.

While on Linux those tools do usually carry some extra options, the ones in macOS do try to be POSIX compliant but do not always succeed.

Therefore, the best way to get bash (or whatever shell you may use such as /bin/zsh or /bin/ksh) to behave the same way on both environments is to install GNU command like tools on macOS.

Install homebrew

If you’re missing Xcode, homebrew will request you to install before proceeding.

Run the following command in order to get homebrew installed:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Then modify your $PATH in order to include the GNU binaries you’re about to install:

Add the following line to your .bashrc / .zshrc / .whateverrc

export PATH="$(brew --prefix coreutils)/libexec/gnubin:/usr/local/bin:$PATH"

Install GNU command line tools

Stat by installing coreutils, which contains tools such as ls or cat.

brew install coreutils

Then you may probably want to install the most common binaries (appends “–with-default-names” if you want to prevent Homebrew from prepending a g to each of the newly installed commands)

brew install binutils
brew install diffutils
brew install ed --with-default-names
brew install findutils --with-default-names
brew install gawk
brew install gnu-indent --with-default-names
brew install gnu-sed --with-default-names
brew install gnu-tar --with-default-names
brew install gnu-which --with-default-names
brew install gnutls
brew install grep --with-default-names
brew install gzip
brew install screen
brew install watch
brew install wdiff --with-gettext
brew install wget

In addition, there are some GNU commands which are already present in macOS, but you may want to update them:

brew install bash
brew install emacs
brew install gdb  # gdb requires further actions to make it work. See `brew info gdb`.
brew install gpatch
brew install less
brew install m4
brew install make
brew install nano

Updating $MANPATH

You may also want to add $HOMEBREW_PREFIX/opt/coreutils libexec/gnuman to $MANPATH env variable, where $HOMEBREW_PREFIX should return Homebrew’s default $PATH, which is /usr/local by default.