grep : ‘+’ special character

At that link to the POSIX specification you gave, you can read:

An ordinary character is a BRE that matches itself: any character in the supported character set, except for the BRE special characters listed in BRE Special Characters.

The interpretation of an ordinary character preceded by a ( ‘\’ ) is undefined, except for:

  • The characters ‘)’, ‘(‘, ‘{‘, and ‘}’
  • The digits 1 to 9 inclusive (see BREs Matching Multiple Characters)
  • A character inside a bracket expression

So basically, since + is an ordinary BRE character, the behaviour of grep 'x\+' is unspecified, some implementations like GNU grep treat it the same as grep 'x\{1,\}' (grep -E 'x+'), some the same as grep 'x+' some may treat is the same as grep 'x\\+' or anything else.

So if you mean to match the string x\+ portably, you should write grep 'x\\+' (or grep 'x[\]+', or grep -F 'x\+' or grep -E 'x\\\+' or grep -E 'x[\][+]').

You may Also Like:

None found

Leave a Comment