grep - 为什么 grep 模式中的括号会从 ps 结果中删除 grep 过程?

14 command-line grep ps

为什么grep模式中的括号会从ps结果中删除 grep 过程?

$ ps -ef | grep XXXX

[...] XXXX
[...] grep XXXX


$ ps -ef | grep [X]XXX

[...] XXXX
Run Code Online (Sandbox Code Playgroud)

Ste*_*fan 23

运行时ps -ef | grep string,grep 会显示在输出中,因为string匹配[...] grep string

但是,当您运行ps -ef | grep [s]tring该行时,不会显示该行,因为 grep 转换[s]tringstring,而 ps 输出[...] grep [s]tring,并且不匹配string


小智 1

因为括号需要转义,对于 bash 一次,对于 grep 再次:

$ ps -ef | grep \\[X\\]XXX

[...] XXXX
[...] grep XXXX


$ ps -ef | grep "\[X\]XXX"

[...] XXXX
[...] grep XXXX
Run Code Online (Sandbox Code Playgroud)