在grep中用双引号括起reg-ex和提供reg-ex之间的区别

时间:2016-03-08 07:09:07

标签: regex bash

这是ls -R命令的输出:

.:
compare.sh   searchByFile.sh   startup.sh   temp.txt  test.sh
compare.sh~  searchByFile.sh~  startup.sh~  test      test.sh~

./test:
test.c  test.txt  test.txt~

我想隔离扩展名为“.c”的文件

所以我这样做:ls -R | grep \.c这是我得到的输出:

searchByFile.sh
searchByFile.sh~
test.c

但是当我这样做时:ls -R | grep "\.c"我得到了正确的输出:

test.c

为什么会这样。 grep "\.c"grep \.c

之间有什么区别?

1 个答案:

答案 0 :(得分:4)

可以很容易地说明这种差异
$ echo "\.c" \.c
\.c .c
$

在双引号内,反斜杠传递给命令 - echogrep。外部双引号,反斜杠未通过。这解释了差异。

其他方面,请参阅quoting上的Bash手册。