这是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
答案 0 :(得分:4)
可以很容易地说明这种差异
$ echo "\.c" \.c
\.c .c
$
在双引号内,反斜杠传递给命令 - echo
或grep
。外部双引号,反斜杠未通过。这解释了差异。
其他方面,请参阅quoting上的Bash手册。