如何在 Mac OS 上安装 GNU grep?

pys*_*seo 7 macos homebrew grep

我需要在我的 Mac 上安装 GNU grep,但我发现了一些困难。

我尝试这样做:

brew install grep --with-default-names
Run Code Online (Sandbox Code Playgroud)

但这不再是一种选择,因为 Homebrew 删除了--with-default-names.

任何人都可以为此提供解决方案吗?

Gin*_*pin 12

是的,--with-default-names删除了

但是有些公式对此有解决方法。对于grep,您可以查看brew info grep安装和使用说明。

...
==> Caveats
All commands have been installed with the prefix "g".
If you need to use these commands with their normal names, you
can add a "gnubin" directory to your PATH from your bashrc like:
  PATH="/usr/local/opt/grep/libexec/gnubin:$PATH"
...
Run Code Online (Sandbox Code Playgroud)

首先,要安装,install不用--with-default-names.

$ brew install grep
...
==> Summary
  /usr/local/Cellar/grep/3.3: 21 files, 880.7KB
Run Code Online (Sandbox Code Playgroud)

您还应该获得与我提到的相同的警告信息。现在,默认情况下,Homebrewgrep将以“g”为前缀,因此它可以作为ggrep.

$ ggrep -V
ggrep (GNU grep) 3.3
Packaged by Homebrew
Copyright (C) 2018 Free Software Foundation, Inc.
...
Run Code Online (Sandbox Code Playgroud)

这可以防止它遮蔽grepMac 附带的内置功能。

$ grep -V
grep (BSD grep) 2.5.1-FreeBSD
Run Code Online (Sandbox Code Playgroud)

如果您确实需要使用grep而不是ggrep,只需按照说明将/usr/local/opt/grep/libexec/gnubin放在PATH. 您必须在您的.bashrc.bash_profile(无论您使用哪个)中执行此操作。

$ grep -V
grep (BSD grep) 2.5.1-FreeBSD
Run Code Online (Sandbox Code Playgroud)