ハイパーマッスルエンジニア

Vim、ShellScriptについてよく書く

Macでggrepをgrepとして扱う

f:id:rasukarusan:20190406001900p:plain:w400

GNU版のgrepをインストールするとggrepになる

brew install grepでインストールしたgrepggrepとして登録される。
ggrepではなくgrepとして扱いたい場合、PATHを追加してあげるといい

PATHを追加

.zshrcに以下を記載

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

上記のPATHは以下のコマンドで確認できる

$ brew info grep
grep: stable 3.3 (bottled)
GNU grep, egrep and fgrep
https://www.gnu.org/software/grep/
/usr/local/Cellar/grep/3.3 (21 files, 880.7KB) *
  Poured from bottle on 2019-04-05 at 21:57:03
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/grep.rb
==> Dependencies
Build: pkg-config ✔
Required: pcre ✔
==> 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"
==> Analytics
install: 5,374 (30 days), 19,977 (90 days), 60,739 (365 days)
install_on_request: 4,820 (30 days), 17,480 (90 days), 53,907 (365 days)
build_error: 0 (30 days)

終わり

--with-default-namesを付けて再インストールすると良い、とよく見かけるが、最近のbrewはこのオプションがない。
また、grepではなくcoreutilsのPATHを追加すれば良い、と書いてあるのもあったが、私の環境ではcoreutilsの中(/usr/local/opt/coreutils/libexec/gnubin)にgrepが含まれていなかったので意味なし。

coreutilsのPATHを取得するためにbrew info coreutilsとした際に、「もしかしてgrepもbrew infoにPATH書いてあるんじゃね」と思いやってみたらドンピシャだったという感じ。
かなり適当である。