I'm trying to understand how grep deciding to take $ as regex, or as a searchable character. My results show that grep is inconsistently deciding what $ means.

As such, I made a basic example:

$ cat testfile

Here I want to see if I can grab the line ending in "illy" using regex:

$ grep 'illy$' testfile

It seems to work, and it is interpreting the 'illy$' criteria as the $ being a regex to say "match this expression at the end of the line". So my thinking now, is that grep interprets $ as a regex criteria, and not a string. So if I wanted to try and grep for '$illy', then this should not match anything because it's interpreting $ as regex.


$ grep '$illy' testfile

Why does it find this line? This proves that even grep doesn't know how to interpret the character $. Let alone, myself trying to understand it.

1 Answer 1


grep by default uses Basic Regular Expressions (BRE), and $ is a special character in BRE's only at the end of an expression.

If you want grep to process pattern as an extended regexp, use -E option

-E, --extended-regexp
              Interpret PATTERN as an extended regular  expression  (ERE,  see


pilot6@Pilot6:~$ grep '$illy' test
pilot6@Pilot6:~$ grep -E '$illy' test

pilot6@Pilot6:~$ grep 'illy$' test
pilot6@Pilot6:~$ grep -E 'illy$' test
  • I don't have enough knowledge on this matter to elaborate. I always use -E when I want a regexp as a pattern.
    – Pilot6
    Commented Feb 28, 2019 at 14:00
  • This isn't answering my question and is removing focus away from what I'm trying to identify as a problem, or something I don't understand with regards to grep (and not egrep). Why is grep non-deterministic in it's decision? (just so you know, I fully agree with your use of egrep, but I am specifically trying to work out how normal grep is working here).
    – john smith
    Commented Feb 28, 2019 at 14:01
  • The @steeldriver link explains it.
    – Pilot6
    Commented Feb 28, 2019 at 14:03
  • I copied a part of it.
    – Pilot6
    Commented Feb 28, 2019 at 14:05
  • This is brilliant. My first time using regex with grep, and I discover that grep uses $ as regex, except when it decides not to lol. I can't mark steeldrivers answer as correct because it's only a reply.
    – john smith
    Commented Feb 28, 2019 at 14:06

You must log in to answer this question.

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