My computer is running macOS 10.12.3 and I am using the system-installed grep utility with version 2.5.1-FreeBSD.

These are the outputs I get when testing various regexes:


But if I run these using GNU grep (version 2.25) I get the following:


GNU's grep looks correct to me and BSD's is incorrect, no? Why do they differ? I don't understand why BSD's grep is matching the "a" when the regex specifies that it must follow the beginning of the line.

  • 2
    Feel free to report the first behaviour as a bug. – Satō Katsura Mar 22 '17 at 6:25
  • 1
    You'll see that echo 'bababa' | grep -E '^(a|b)' is all red. And you'll have the same problem with -o. I guess, grep looks for multiple matches, but for the second match and over, it applies the regexp to what's on the right of what matched the previous time (so on anana on the second time in your case) without passing the REG_NOTBOL flag. See also echo banana | grep -Eo '^ban|^ana$' – Stéphane Chazelas Mar 22 '17 at 7:02
  • 2
    @JdeBP, on my OS X, grep identifies itself as grep (BSD grep) 2.5.1-FreeBSD with the man page referring to GNU only by mentioning "long options provided for compatibility with GNU versions". Also, no copyright notice or mentions of GPL (or FSF). If it has history with the GNU util, it's hard to tell. – ilkkachu Mar 22 '17 at 9:27
  • We're demonstrating how this is misleading. (-: The grep that is discussed in Decade Moon's answer that so many people are thinking is correct identifies as grep (GNU grep) 2.5.1-FreeBSD (as can be seen in the bug discussion), has a FSF copyright notice, and is most definitely GNU grep. What FreeBSD calls bsdgrep is what OpenBSD calls grep and is BSD grep, with no --color option outwith the FreeBSD variant. That is what identifies as grep (BSD grep) 2.5.1-FreeBSD, and ironically is what one person in the bug discussion suggested that FreeBSD switch grep to. – JdeBP Mar 22 '17 at 13:50

I think this might be a bug in FreeBSD's grep. There's a bug report with similar issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.