Install GNU sed on Mac OS and Set It as Default

The workaround on 2020

Xu LIANG
2 min readMay 20, 2020

We know that the sed on Mac OS is the POSIX sed, which cannot use many options. On the other hand, GNU sed is very convenient. For example, GNU sed interprets escape sequences like \t, \n, \001, \x01, \w, and \b. OSX's sed and POSIX sed only interpret \n (but not in the replacement part of s). See Differences between sed on Mac OSX and other “standard” sed? for more detail.

Another thing is that the --with-default-names option is removed since January 2019, so now we cannot use brew install gnu-sed --with-default-names to replace the sed directly.

In this post, we will replace the OSX’s sed with GNU sed and set it as default.

Install

We first confirm the path of sed and install GNU sed with brew.

$ which sed              
/usr/bin/sed
$ brew install gnu-sed

Next, we use brew info to see the GNU sed information.

$ brew info gnu-sed
...
GNU "sed" has been installed as "gsed".
If you need to use it as "sed", you can add a "gnubin" directory
to your PATH from your bashrc like:
PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"
...

--

--

Xu LIANG

I’m an engineer focusing on NLP and Data Science. I write stuff to repay the engineer community. You can find me on linkedin.com/in/xu-liang-99356891/