Sample Header Ad - 728x90

Why does the special character `?` need to be escaped in grep, but not `.` or `*`?

8 votes
2 answers
1193 views
The only file in my current working directory is called test.txt and its content is simply:
This is a little test file.
* Executing
-sh
    grep -in * -e 'te.?t file'
gives **no match**. * But escaping ? works:
-sh
    grep -in * -e 'te.\?t file'
**gives match** **Crazy:** With the star *, things are the other way round! * Without escaping:
-sh
    grep -in * -e 'te.*t file'
**gives match** * and escaping * does not work:
-sh
    grep -in * -e 'te.\*t file'
**no match** Why are the meta-characters ? and * treated differently with respect to escaping?
Asked by Dominic van der Zypen (195 rep)
Nov 21, 2024, 01:51 PM
Last activity: Nov 28, 2024, 01:56 PM