Installed GNU grep on OSX, but can't use

2020-07-09 09:43发布

问题:

I've tried installing GNU grep on OSX, and it seems to be installed, but I can't use it.. I've done so using homebrew, Macports is having some issues currently, so I can't use that.

To install: brew tap homebrew/dupes; brew install grep

Which returns: Warning: homebrew/dupes already tapped! Warning: homebrew/dupes/grep-2.21 already installed

Symlinking seems to work to /usr/local/bin/ggrep. When I add the alias alias grep="ggrep" and do grep --version, I get -bash: ggrep: command not found. Which is true, since there is no ggrep in the folder. I've tried installing with and without --with-default-names.

The folder /usr/local/Cellar/grep/2.21/bin/ contains the following:

-r-xr-xr-x 1 Wes admin 158 Oct 14 09:27 egrep
-r-xr-xr-x 1 Wes admin 158 Oct 14 09:27 fgrep

Which is strange to me, since the documentation implies that The command has been installed with the prefix "g".

I've seen the following post, but none of the solutions work for me. Updating grep for Mac OS 10.7

Does anyone have any solutions? I really want to use GNU grep.

Output of brew unlink grep && brew link grep -v:

Unlinking /usr/local/Cellar/grep/2.21...
6 symlinks removed
Linking /usr/local/Cellar/grep/2.21...
ln -s ../Cellar/grep/2.21/bin/egrep egrep
ln -s ../Cellar/grep/2.21/bin/fgrep fgrep
ln -s ../../Cellar/grep/2.21/share/info/grep.info grep.info info /usr/local/share/info/grep.info
ln -s ../../../Cellar/grep/2.21/share/man/man1/egrep.1 egrep.1
ln -s ../../../Cellar/grep/2.21/share/man/man1/fgrep.1 fgrep.1
ln -s ../../../Cellar/grep/2.21/share/man/man1/grep.1 grep.1
6 symlinks created`

New: brew uninstall grep; brew install grep

$ which -a grep
/usr/bin/grep

$ which -a ggrep
/usr/local/bin/ggrep

$ echo $PATH
/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/git/bin

This time, it seems something is different. ggrep is finally installed! I think the unlink/link straightened some things out. How can I set ggrep as the default? With alias?

回答1:

To make GNU grep the default install it with --with-default-names:

$ brew install grep --with-default-names

If you already have it installed use reinstall instead of install.

Ensure that /usr/local/bin (the location of GNU grep) is before /usr/bin (the location of the BSD grep) in your $PATH; which seems to be the case here.

You might have to start a new shell session afterward because Bash caches the binaries paths for the current session. This means that the first time you use grep it’ll determine which binary it’ll use depending on your $PATH and cache it. The next time it’ll use the cached value so changing your $PATH won’t change anything until you reload the shell.



回答2:

Offically out of date for the answer above.

As of Homebrew version 2.0.0 the --with-default-names flag is no longer available.

from the official documentation

--with-default-names is no longer supported. It is now installed into its own directory and you will need to adjust your PATH to use it.

What you need to do is to add this command to your shell

PATH="/usr/local/opt/grep/libexec/gnubin:$PATH"


回答3:

As of Febuary 8, 2020, You could either prepend the PATH and MANPATH variables as eleijonmarck proposes, or use symbolic links to a path loaded early on the PATH. If you choose to use eleijonmarck's method, I would do it differently.

You need to know where Homebrew installs grep binaries.

PATH=/usr/local/opt/grep/bin:$PATH

as opposed to libexec. The Filesystem Hierarchy specifies that the libexec directory

includes internal binaries that are not intended to be executed directly by users or shell scripts.

Also, if you are doing that, then you might want the MAN pages too.

MANPATH=/usr/local/opt/grep/share/man:$MANPATH

The other option is to use symbolic links. You might think to do this:

ln -s /usr/local/opt/grep/bin/grep /usr/local/bin/

The thing is, this won't work, because MacOS has grep as a built-in. Instead, would need to remove or replace the built-in at /usr/bin. You could rename it mv /usr/bin/grep /usr/bin/bsdgrep. This effectively makes grep not findable at /usr/bin, but still usable as bsdgrep (advantage of this technique). Assuming you added grep to /usr/local/bin, you could test your change with:

which grep && grep -V # prove old grep found first
which grep && grep -V # prove new grep found first

The disadvantage of this technique is that you must add a symbolic link per binary.



标签: path homebrew