14

Why do brackets in a grep pattern remove the grep process from ps results?

$ ps -ef | grep XXXX

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


$ ps -ef | grep [X]XXX

[...] XXXX
Kusalananda
  • 320,670
  • 36
  • 633
  • 936

2 Answers2

23

When you run ps -ef | grep string, grep is displayed in the output because string matches [...] grep string.

But, when you run ps -ef | grep [s]tring the line isn't displayed, because grep translates [s]tring to string, while ps outputs [...] grep [s]tring, and that doesn't match string

Stefan
  • 24,830
  • 40
  • 98
  • 126
1

Because the brackets need to be escaped, for bash once and for grep again:

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

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


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

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