Question or issue on macOS:
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’
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.
How to solve this problem?
Solution no. 1:
--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
You already have the gnu-sed installed without the
--with-default-namesoption it installs
- Without that option it installs
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'
gsed as others suggested.
Solution no. 2:
When you install the GNU version of
sed for Mac OS X using:
$ brew install gnu-sed
The program that you use is
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
Solution no. 3:
sed that ships with OS X is in
homebrew installs is in
If you prefer to use the
homebrew one, you have two options:
Every time you want to use
/usr/local/bin/ ahead (i.e. before)
/usr/bin in your PATH in your login profile, like this
Solution no. 4:
$ brew install gnu-sed
$ export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"
With these two commands gnu-sed works properly
Solution no. 5:
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.
Solution no. 6:
--with-default-names didn’t work for me on Mac OS X 10.14.2 so I created a symlink named
gsed higher in the
I also created a symlink named
sed.1 to the
gsed.1 manpage higher in the
man would access the
gsed manpage instead of the default