Based on this question on SO, I have discovered that:

echo "a']" | grep -E "\a"

Matches OK.

echo "a']" | grep -E "\]"

Matches OK.


echo "a']" | grep -E "\'"

Does not match. I cannot find any documentation to describe how exactly grep -E handles escaping a single quote. I am using GNU grep 2.16 on Ubuntu 14.04.


echo "a']" | grep -E "'"

Matches OK, but I am curious as to how grep -E is interpreting \'.


I've just tried this on Cygwin 2.6.1 and grep 2.27 and I can reproduce. Using grep --colour -Eo perhaps gives a clearer OK/Fail result?

  • 3
    On a similar (14.04, grep 2.16) system, I can not reproduce, grepping "\'" does match. I'm not sure what's going on here, ...
    – SYN
    Dec 26, 2016 at 2:02
  • 1
    Your update tells about grep -Eo: note that when I add the -o to my grep, nothing's written to stdout (now that's unexpected, AFAIU/not being familiar with that option) yet grep returns with 0 (not 1), which indicates that something was matched.
    – SYN
    Dec 26, 2016 at 5:06
  • 2
    @SYN: That is the same behavior that you get for grep -o "" or grep -Eo "" — as steeldriver suggested, an empty pattern patches every line, but matches zero characters. Dec 26, 2016 at 5:11

1 Answer 1


It's not just GNU grep, with GNU sed and GNU awk: echo "a']" | sed "s/\'/foo/" -> a']foo and echo "a']" | awk "{sub(/\'/,\"foo\")}1" -> a']foo. When you run awk in POSIX mode it does not do that but instead exhibits the expected behavior: echo "a']" | awk --posix "{sub(/\'/,\"foo\")}1" -> afoo].

The issue is that there is a GNU-ism that \' means the same as $, see http://www.regular-expressions.info/gnu.html:

Additional GNU Extensions
The anchor \` (backtick) matches at the very start of the subject string,
while \' (single quote) matches at the very end. 

If anyone has a use-case where \' is different from $ I'd love to hear it.


You must log in to answer this question.

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