Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install GNU grep on Mac OS?

I need to install GNU grep on my Mac but I'm finding some difficulties.

I tried doing this:

brew install grep --with-default-names

But this is no longer an option since Homebrew removed --with-default-names.

Can anyone provide a solution for this?

like image 977
pyseo Avatar asked Nov 21 '25 04:11

pyseo


1 Answers

Yes, --with-default-names was removed.

But some formulas, like grep, provided a workaround for this:

$ 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"
...

First, to install, just do install without --with-default-names.

$ brew install grep
...
==> Summary
🍺  /usr/local/Cellar/grep/3.3: 21 files, 880.7KB

You should also see that same "Caveats" info I mentioned at the start. Now, by default, the Homebrew grep would be prefixed by a "g", so it's accessible as ggrep.

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

This prevents it from shadowing the built-in grep that comes with Mac.

$ grep -V
grep (BSD grep) 2.5.1-FreeBSD

If you really need to use grep and not ggrep, just follow the instructions and put /usr/local/opt/grep/libexec/gnubin at the start of your PATH. You have to do this in your .bashrc or .bash_profile (whichever one you use).

$ echo 'export PATH="/usr/local/opt/grep/libexec/gnubin:$PATH"' >> ~/.bash_profile
$ source ~/.bash_profile
$ grep -V
grep: warning: GREP_OPTIONS is deprecated; please use an alias or script
grep (GNU grep) 3.3
Packaged by Homebrew
...
like image 172
Gino Mempin Avatar answered Nov 24 '25 04:11

Gino Mempin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!