如何在 Mac OS 10.10 + 上使用 GNU sed,‘ brewinstall —— default-name’不再支持

在 Mac OS 10.10.3下,我安装 gnu-sed 时输入:

brew install gnu-sed --default-names

当我再次输入时,我得到的信息是:

Gnu-sed-4.2.2已经安装

然而,即使在重新启动系统和重新启动终端之后,我仍然不能使用 sed 的 GNU 版本:

echo a | sed ’s_A_X_i’

报税表: 替换命令‘ i’中的错误标志

我应该怎么做才能让 GNU 版本正常工作? 下面是 $PATH 变量中的路径。

/Users/WN/-myUnix
/opt/local/bin
/opt/local/sbin
/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/bin
/Applications/calibre.app/Contents/MacOS
/opt/ImageMagick/bin
/usr/texbin

如果我的问题看起来很明显,我很抱歉,但是我正在自己学习 shell 脚本,并且还不完全理解 UNIX 程序是如何安装的。如果您能帮助我在 Mac 上使用遵循 GNU 的命令(在本例中为 sed,但很快我还需要其他命令) ,而不造成损坏或不必要的混乱,我将不胜感激。

93873 次浏览

The sed that ships with OS X is in /usr/bin.

The sed that homebrew installs is in /usr/local/bin.

If you prefer to use the homebrew one, you have two options:

Option 1

Every time you want to use homebrew sed, type

/usr/local/bin/sed

or, preferably

Option 2

Move /usr/local/bin/ ahead (i.e. before) /usr/bin in your PATH in your login profile, like this

 export PATH=/usr/local/bin:<other places>

If you need to use gnu-sed command with their normal names, you can add a "gnubin" directory to your PATH from your bashrc. Just use the following command in your bash or terminal.

export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"

When you install the GNU version of sed for Mac OS X using:

$ brew install gnu-sed

The program that you use is gsed.

So for example:

$ echo "Calimero is a little chicken" > test
$ cat test
Calimero is a little chicken
$ gsed -i "s/little/big/g" test
$ cat test
Calimero is a big chicken

Also, to compliment the use of GNU command tools on Mac OS X, there is a nice blog post here to get the tools from linux:

Install and use GNU command line tools on Mac OS/OS X

Note (2019):

The --with-default-names option is removed since January 2019, so now that option is not available anymore.

When installing, Homebrew instructs on how to adapt the path, if one wants to use sed without the g prefix.


You already have the gnu-sed installed without the --with-default-names option.

  • With --with-default-names option it installs sed to /usr/local/bin/
  • Without that option it installs gsed

So in your case what you gotta do is:

$ brew uninstall gnu-sed
$ brew install gnu-sed --with-default-names

Update path if needed...

$ echo $PATH | grep -q '/usr/local/bin'; [ $? -ne 0 ] && export PATH=/usr/local/bin:$PATH
$ echo a | sed 's_A_X_i'

or use gsed as others suggested.

--with-default-names didn't work for me on Mac OS X 10.14.2 so I created a symlink named sed to gsed higher in the $PATH

I also created a symlink named sed.1 to the gsed.1 manpage higher in the $MANPATH so man would access the gsed manpage instead of the default sed manpage

$ brew install gnu-sed

$ export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"

With these two commands gnu-sed works properly

this export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"

is only valid per terminal SESSIOn as soon as you restart its GONE ( Mojave )

Since the --with-default-names option was removed in Jan. 2019, my hack solution is:

# hack to override mac tools with homebrew versions (ls, sed, etc)
for p in `find "${HOMEBREW_PREFIX}" -type d -name gnubin` ; do
export PATH="${p}:${PATH}"
done

which is a little slow (crawling the dir every login) but works without forcing me to modify my .bashrc for every gnu tool I happen to install with brew.

A slightly faster way to do what @pjz suggests is the following:

for p in $(ls -d ${HOMEBREW_PREFIX:-/usr/local}/Cellar/*/*/libexec/gnubin); do
export PATH="${p}:${PATH}"
done

Of course this assumes every GNU package in brew will always have the same level of directories to get to gnubin.

Alternatively, you can speed up find by adding the -maxdepth 4 flag before -type d to reduce how far it has to do into directories.

When running brew install gnu-sed on latest homebrew it reports at the end:

==> Caveats
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="/opt/homebrew/opt/gnu-sed/libexec/gnubin:$PATH"

gnu-sed installs by default as gsed. However, if you look in /opt/homebrew/opt/gnu-sed/libexec/gnubi you'll find a sed command. So following the above instructions to update the path should mean sed runs gnu-sed.