HOME  → POSTS  → 2019

Using GNU command line tools in macOS instead of FreeBSD tools

Software375 words2 minutes to read

macOS is a Unix, and not built on Linux. This can bite you when trying to write scripts which support both platforms.

macOS

I think most of us realize that macOS isn’t a Linux OS, but what that also means is that instead of shipping with the GNU flavor of command line tools, it ships with the FreeBSD flavor. As such, writing shell scripts which can work across both platforms can sometimes be challenging.

Homebrew

Homebrew can be used to install the GNU versions of tools onto your Mac, but they are all prefixed with “g” by default.

NOTE: All commands have been installed with the prefix “g”. If you need to use these commands with their normal names, you can add a “gnubin” directory to your PATH from your bashrc.

Choosing GNU for Consistency

You can install most of the GNU flavored tools with:

brew install coreutils ed findutils gawk gnu-sed gnu-tar grep make

Assuming you have a fairly standard Terminal/shell environment, and assuming that you want to use the GNU versions instead of the BSD versions for everything you’ve installed with Homebrew, you can append the following to your ~/.profile file.

# Get list of gnubin directories
export GNUBINS="$(find /usr/local/opt -type d -follow -name gnubin -print)";

for bindir in ${GNUBINS[@]}; do
  export PATH=$bindir:$PATH;
done;

Provided Binaries

Package Provides
coreutils [, b2sum, base32, base64, basename, basenc, cat, chcon, chgrp, chmod, chown, chroot, cksum, comm, cp, csplit, cut, date, dd, df, dir, dircolors, dirname, du, echo, env, expand, expr, factor, false, fmt, fold, groups, head, hostid, id, install, join, kill, link, ln, logname, ls, md5sum, mkdir, mkfifo, mknod, mktemp, mv, nice, nl, nohup, nproc, numfmt, od, paste, pathchk, pinky, pr, printenv, printf, ptx, pwd, readlink, realpath, rm, rmdir, runcon, seq, sha1sum, sha224sum, sha256sum, sha384sum, sha512sum, shred, shuf, sleep, sort, split, stat, stdbuf, stty, sum, sync, tac, tail, tee, test, timeout, touch, tr, true, truncate, tsort, tty, uname, unexpand, uniq, unlink, uptime, users, vdir, wc, who, whoami, yes
ed ed, red
gawk awk
grep egrep, fgrep, grep
gnu-sed sed
gnu-tar tar
make make
findutils find, locate, updatedb, xargs

Conclusion

Using the GNU flavor of command line tools (instead of the FreeBSD flavor) should simplify the task of writing shell scripts which can work across macOS and GNU/Linux.

Ryan Parman

is an engineering manager with over 20 years of experience across software development, site reliability engineering, and security. He is the creator of SimplePie and AWS SDK for PHP, patented multifactor-authentication-as-a-service at WePay, defined much of the CI/CD and SRE disciplines at McGraw-Hill Education, and came up with the idea of “serverless, event-driven, responsive functions in the cloud” while at Amazon Web Services in 2010. Ryan's aptly-named blog, , is where he writes about ideas longer than . Ambivert. Curious. Not a coffee drinker.