Quotes when using grep?

后端 未结 4 554
南旧
南旧 2021-02-02 13:15

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:

4条回答
  •  一生所求
    2021-02-02 13:58

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

提交回复
热议问题