Skip to content

Instantly share code, notes, and snippets.

@cmsj
Last active April 28, 2019 13:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmsj/be6df09d09a61ab8b1fd8872dd6e1a77 to your computer and use it in GitHub Desktop.
Save cmsj/be6df09d09a61ab8b1fd8872dd6e1a77 to your computer and use it in GitHub Desktop.
#!/bin/echo Don't execute this script directly, source it into your shell.
# This script will attempt to make your macOS system behave more like GNU for shell scripts
# by diverting as many commands as possible, to the GNU versions, provided by Homebrew
#
# For most Homebrew tools, their presence in /usr/local/bin and that being in your $PATH
# means you can use them as-is, but some tools appear with different names because macOS
# already has such a tool (e.g. find(1)). These tools generally also have a `gnubin` path
# which we can use to make them higher priority than the macOS equivalents.
#
# To use this:
# 1) Install various GNU tools via homebrew, such as:
# * coreutils
# * gnu-getopt
# * gnu-sed
# * gawk
# * gnu-tar
# * gnu-time
# * gnu-which
# * moreutils
# * num-utils
# 2) Load this script into your shell when you want to Go GNU:
# * source /path/to/homegnu.sh
# 3) (optional extra) build something fancy into your $PS1 to detect $__HOMEGNU is set, to let you know commands are different now
# 4) Explore other packages that have gnubin paths: grep -rl gnubin /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/
# 5) To undo Homegnu, either set your $PATH back to normal, or just spawn a new shell/terminal
BREWROOT="/usr/local"
GNUPATH=""
while read -r gnubin ; do
GNUPATH="${gnubin}:${GNUPATH}"
done < <(find -L "${BREWROOT}/opt" -type d -name gnubin)
export PATH="${GNUPATH}${PATH}"
export __HOMEGNU=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment