I have coreutils but I am not sure if GNU grep is there. I just want to use the flag -P for Perl regex that is found in GNU grep, but not in BSD grep.

My PATH is /usr/local/opt/coreutils/libexec/gnubin:/usr/local/bin:/usr/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin:/usr/texbin:/Users/masi/.cabal/bin so I have coreutils first in the PATH.

However, grep is BSD when I am using it: grep --version gives grep (BSD grep) 2.5.1-FreeBSD.

Command type -p grep returns /usr/bin/grep

How can you install GNU Grep in OSX?


GNU grep is not part of coreutils. To install, run

brew install grep

As with coreutils, this doesn't automatically replace the existing grep

==> Caveats
The command has been installed with the prefix "g".
If you do not want the prefix, install using the "with-default-names" option.
  • 2
    When I do this my grep is still the freeBSD one, but egrep and fgrep are GNU. How can I set the default grep to GNU? (I did use --with-default-names) – Juicy Oct 16 '16 at 15:32
  • 3
    @juicy did you try this in a new shell? – nohillside Oct 16 '16 at 17:47
  • 4
    For reference, the GNU grep command is named ggrep – Will Sheppard Feb 21 '18 at 14:18
  • 2
    Run brew uninstall grep (or brew remove grep) if installed already and brew install grep --with-default-names to give GNU grep precedence on the command line. Tested with Homebrew 1.5.4 Homebrew/homebrew-core (git revision 3bb326; last commit 2018-02-22) Do not forget to open a new shell or run hash -r or export PATH=$PATH to reload the paths to binary applications (i.e. your new GNU grep). – Jonathan Komar Feb 22 '18 at 8:51
  • 5
    @JonathanKomar unfortunately in Homebrew version 2.0.0 the --with-default-names flag is no longer available. – Dez Feb 6 '19 at 15:39

As the comments indicate, the highest rated answer (from nohillside) needs updating as follows:

If grep was already installed by brew, remove grep first.

% brew uninstall grep

Then install grep:

% brew install grep

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:
=º  /usr/local/Cellar/grep/3.3: 21 files, 880.7KB

Note that you do need to modify the PATH. For example, add to your .bashrc:

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

I had to do the above on my mac when after brew upgrade my grep was no longer accessible (it was installed previously with brew install grep --with-default-names, and this option is not available any more).

This solution works as of Homebrew 2.1.1:

brew --version
Homebrew 2.1.1
Homebrew/homebrew-core (git revision 5afdd; last commit 2019-04-22)
Homebrew/homebrew-cask (git revision a5a206; last commit 2019-04-22)

This answer is based on the one from nohillside, with comments from Jonathan Komar and scott m gardner.

  • 1
    This is better than the accepted answer. – smci Oct 12 '19 at 3:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .