0

This is a really simple question, but even after reading about wildcards/regex, I can't seem to grasp entirely what the differences between these commands are:

grep . test.txt

this one will look for lines containing any character in test.txt

grep \. test.txt

this one also apparently does the exact same thing, even though I assumed it shouldn't, since it would be escaped and return only those lines containing an actual '.'

grep ? test.txt

since ? is a special character, I guess it would be interpreted as a wildcard, but since grep doesn't deal with wildcards, it's not going to output anything; however

grep \? test.txt

does find the lines which contain an '?' in them, but also those that contain an '\?' in them. Why is this?

jasonwryan
  • 71,734
  • 34
  • 193
  • 226
  • 4
    In `grep \.`, the shell sees the backslash as an escape character, but a dot doesn't need escaping, so it's converted to `.`. So both commands look the same to `grep`. Try `grep '\.'`. – Mikel May 24 '15 at 19:17
  • 2
    For `?` there are several things to point out. First, `grep` gets to see the unescaped `?` because you don't have one-character filenames in the current directory. Second, `\?` is expanded by the shell to `?`. And third, `?` has no special meaning for `grep`. It starts being special for `egrep`, or for `grep -E`, or `grep -P`. Plain `grep` expects basic regexps (BRE), and `?` is nor special in the BRE dialect. – lcd047 May 24 '15 at 19:29

0 Answers0