How to use GNU sed on Mac OS 10.10+, 'brew install --default-names' no longer supported
MacosBashSedHomebrewGnuMacos Problem Overview
Under Mac OS 10.10.3, I installed gnu-sed by typing:
brew install gnu-sed --default-names
When I type it again, I get the message:
gnu-sed-4.2.2 already installed
However, even after rebooting the system and restarting Terminal, I still cannot use the GNU version of sed. For example:
echo a | sed ’s_A_X_i’
returns: bad flag in substitution command 'i'
What should I do to get the GNU version working? Here are the paths in my $PATH variable.
/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
I'm sorry if my question seems obvious, but I am learning shell scripting on my own and don't quite understand yet how UNIX programs are installed. Any help to use GNU compliant commands (in this case sed, but soon I'll need others as well) on my Mac without causing damage or unnecessary clutter would be greatly appreciated.
Macos Solutions
Solution 1 - Macos
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 installssed
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.
Solution 2 - Macos
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][1]
[1]: https://www.topbug.net/blog/2013/04/14/install-and-use-gnu-command-line-tools-in-mac-os-x/ "Install and Use GNU Command Line Tools on macOS/OS X"
Solution 3 - Macos
$ brew install gnu-sed
$ export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"
With these two commands gnu-sed works properly
Solution 4 - Macos
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>
Solution 5 - Macos
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"
Solution 6 - Macos
--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
Solution 7 - Macos
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 )
Solution 8 - Macos
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.