请记住grep
是基于线的。如果任何行匹配,则您获得匹配。 (在你的第一种情况下test.zip
匹配(更准确地说:您使用了-v
因此您要求的线条与您的模式不匹配,并且test.zip
正是这样做的,即与您的模式不匹配。结果你的 grep 调用成功了)。比较
$ grep -vE '^[.]' <<<$'.\na'; echo $?
a
0
with
$ grep -vE '^[.]' <<<$'.\n.'; echo $?
1
注意第一个命令如何输出该行a
,即已找到匹配项,这就是退出状态为 0 的原因。将其与第二个示例进行比较,其中没有行匹配。
参考
<<<
是这里的字符串:
Here Strings
A variant of here documents, the format is:
[n]<<<word
The word undergoes brace expansion, tilde expansion, parameter and
variable expansion, command substitution, arithmetic expansion, and
quote removal. Pathname expansion and word splitting are not per-
formed. The result is supplied as a single string, with a newline
appended, to the command on its standard input (or file descriptor n if
n is specified).
$ cat <<<'hello world'
hello world
$'1\na'
用于获取多行输入(\n
被替换为换行符$'string'
,更多请参见man bash
).
$ echo $'1\na'
1
a