Troubleshooting ‘Gsed: Command Not Found’ Error While Using Homebrew on macOS

A blog post is available to get the GNU command tools from Linux to compliment their use on Mac OS X. Two commands are provided in Solution 3 that enable gnu-sed to work properly. In Solution 4, it is mentioned that the default sed on Mac OS X is in a different location. Can you provide me with a list of commands where I should use the GNU command instead of the default, for instance, instead of [default command]? Moreover, what steps should I take to use all GNU commands without any trouble on my Mac?

Question:

Utilizing oh my zsh limits my ability to execute certain commands that were available to me in Ubuntu, like utilizing “s” in regular expressions. To address this, I installed home-brew and attempted to execute

brew install gnu-sed --default-names

, but it did not allow me to use
sed command
. The current state of my $PATH variable is not optimal, and I am unsure of how to rectify it.

/Users/ishansrivastava/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/local/bin:/opt/local/sbin:/opt/X11/bin:/Library/TeX/texbin

Could you provide me with a list of GNU commands that I should use instead of the default on my Mac? I am also interested in learning how to use all the GNU commands without any difficulties. Additionally, could you show me an example of when I should use a GNU command instead of the default, such as

gsed

instead of

sed

?


Solution:

In a previous query, it was pointed out that when one uses Homebrew with
installing GNU
,

sed

is installed and can be found as

/usr/local/bin/sed

.

In the absence of

--with-default-names

, the installation will default to

/usr/local/bin/gsed

.

Depending on the installation (

sed

), the binary selected upon executing (

sed

) relies on your specified

$PATH

. In cases where your

$PATH

matches the given description, GNU

sed

should take priority over the macOS-native

sed

.

If you have previously used the

sed

command in the current shell session, it may be cached by the shell. To clear the cache, you can use

rehash

in

zsh

, or simply open a new terminal.

In cases where a system has a non-GNU native implementation of

sed


sed

, it is recommended to use

sed


gsed

to avoid disrupting any scripts that rely on the behavior specific to the native

sed

. This is why Homebrew on macOS defaults to

sed


gsed

. Similarly, other BSD systems typically use the same naming convention for GNU

sed

and its tools.

Frequently Asked Questions