grep
a
在搜索字母时表现不同。当字母a
包含在搜索条件中时,它不会搜索任何其他字母。但其他角色的情况就不一样了。为什么?!
对于命令 : grep [aeiou] file1
or grep [eioau] file1
or grep [a,e,i,o,u] file1
,它显示以下输出:
Name : file1
a
Run Code Online (Sandbox Code Playgroud)
注1:字母“a”在输出中被突出显示为搜索字符(附截图)。
对于 command : grep [eiou] file1
or grep [e,i,o,u] file1
,它显示以下输出:
this is test file.
Name : file1
Run Code Online (Sandbox Code Playgroud)
注2:字母“e”和“i”在输出中被突出显示为搜索字符(附截图)。
文件 1是:
this is test file.
Name : file1
a
Run Code Online (Sandbox Code Playgroud)
您a
在当前目录中有一个名为的文件。您还没有引用传递给的 RE grep
,所以这就是正在发生的事情:
给定的
grep [aeiou] file1
Run Code Online (Sandbox Code Playgroud)
shell 看到[aeiou]
并且因为它是一个有效的(glob)模式,它试图将它与单个字母文件a
, e
, i
, o
,进行匹配u
。由于它成功,它用它匹配的文件替换参数,a
. 然后将整个结果作为命令执行:
grep a file1
Run Code Online (Sandbox Code Playgroud)
这为您提供了您所看到的输出。我应该指出,如果 shell 不能将你的模式 glob 使它保持不变。这允许[eiou]
在明显[aeiou]
“失败”的地方取得成功。
解决方案是单引号您的非文件参数,如下所示:
grep '[aeiou]' file1
Run Code Online (Sandbox Code Playgroud)