Quotes when using grep?

扶醉桌前 提交于 2019-12-03 07:58:36

问题


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


回答1:


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




回答2:


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




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/3008423/quotes-when-using-grep

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!