regexshellscriptinggrepposix-ere

egrep -o : different behaviour in Linux and MacOS


I have two scripts which I would like to run both in a Linux machine and in a MacOS machine. But a different behaviour of the egrep command makes those scripts generate different outputs.

In particular, this is what happens when I use egrep on Linux (Ubuntu):

$ echo ".test" | egrep "[a-zA-Z0-9]*"
.test
$ echo ".test" | egrep -o "[a-zA-Z0-9]*"
test
$

and this is what happens when I use egrep on MacOS

$ echo ".test" | egrep "[a-zA-Z0-9]*"
.test
$ echo ".test" | egrep -o "[a-zA-Z0-9]*"

$

The first behaviour is what I would expect, the second one (empty output) is unexpected. Perhaps this is a bug in the implementation of egrep with -o option under MacOS?

Or, if the second behaviour is correct as well, do you know a way to obtain the same behaviour for the second case?

I tried to look at the corresponding man pages for the two commands, this is extracted from the Linux man page:

 -o, --only-matching
        Print only the matched (non-empty) parts of a matching line, with each 
        such part on a separate output line.

and this is extracted from the man page for MacOS:

 -o, --only-matching
         Prints only the matching part of the lines.

Although the descriptions seem a bit different, the meaning of the two options seems to be the same, so why is egrep -o behaving differently in MacOS? Am I not considering any subtle aspect of this command?


Solution

  • This depends on how the different grep implementations deal with empty matches ([a-zA-Z0-9]* matches the empty string).

    I wrote a longer text about this over at Unix&Linux.

    In short, should all empty matches be returned? There are infinitely many such matches.