Grep acts differently depending on what kind of quotes I surround the regex with. I can\'t seem to get a clear understanding of why this is. Here is an example of the problem:
The command line including the arguments is processed by the shell before it is executed. You can use echo to see what the shell does:
$ echo grep -e show\( test.txt
grep -e show( test.txt
$ echo grep -e "show\(" test.txt
grep -e show\( test.txt
$ echo grep -e 'show\(' test.txt
grep -e show\( test.txt
So without quotes the backslash gets removed making the "(" a normal character for grep (grep uses basic regex by default, use -E to make grep use extended regex).
I do not believe it is grep that is behaving differently, it is the shell. I'm assuming you are using bash
http://www.faqs.org/docs/bashman/bashref_8.html
Basically the quoted versions are behaving differently on the slash, depending on the quoting mechanism.
Both of the quoted examples would have worked without the slash. For the first one, the shell would escape the ( and pass in just show( to the grep for the pattern.
In order:
grep -e show( test.txt
does not work, because the shell interprets the (
as special, a parenthesis, not just a character, and can't find the closing )
.
These both work:
grep -e 'show(' test.txt
grep -e "show(" test.txt
because the shell treats the quoted text as just text, and passes it to grep.
These do not work:
grep -e 'show\(' test.txt
grep -e "show\(" test.txt
because the shell passes show\(
to grep, grep sees \(
as special, a parenthesis, not just a character, and can't find the closing \)
.
The quotes change what grep sees. The backslash (\) in the un-quoted form is processed by the shell, which treats characters after the backslash as special. This happens before grep gets the parameter. grep sees show(. When the quotes (single or double) are used, the shell interprets them as "leave the contents alone", thus grep sees show\( and the \( characters have meaning in grep and it is looking for the closing parenthesis - \).
BTW: Single and double quote handling is different in how the shell handles shell variables, but there are no shell variables in your example.