tags:

views:

171

answers:

4

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:

hamiltont$ grep -e show\(  test.txt 
  variable.show();
  variable.show(a);
  variable.show(abc, 132);
  variableshow();
hamiltont$ grep -e "show\("  test.txt 
grep: Unmatched ( or \(
hamiltont$ grep -e 'show\('  test.txt 
grep: Unmatched ( or \(

I am just assuming there is some proper way to enclose the regex with single/double quotes. Any help?

FWIW, grep --version returns grep (GNU grep) 2.5.1

A: 

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.

W Devauld
+3  A: 

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).

Florian Diesch
Got it, thanks!
Hamy
+1  A: 

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.

Scott Thomson
Got it, thanks!
Hamy
+1  A: 

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 \).

Beta