因此,以下是我运行的命令:
$ ~/Documents: touch ball.txt bool-txt bowl.txt bull.txt
$ ~/Documents: ls . | grep b..l\.txt
ball.txt
bool-txt
bowl.txt
bull.txt
Run Code Online (Sandbox Code Playgroud)
我没想到bool-txt会出现在输出中,因为我已经避开了点,但它仍然没有将其视为字面意义的点。我哪里错了?
我确定这是某个问题的重复,但我找不到它。在 Unix SE https://unix.stackexchange.com/questions/583428/why-do-i-have-to-quote-an-escaped-character-in-a-regular-expression-for-grep-bu
当您使用 时grep b..l\.txt,shell 会转换\.为.grep 接收b..l.txt,然后它会解释.为任何字符。
为了防止这种情况,最简单的方法是给 grep 一个类似 的字符串grep 'b..l\.txt',然后外壳就不会转义点。请注意,单引号和双引号在 bash 中做不同的事情,但在这种情况下无关紧要。或者,使用双反斜杠,以便 shell 将反斜杠作为文字字符进行转义,并且 grep 正确接收\..