Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question
2  
This is a great little trick, in the first place! –  Kevin Cantu Oct 15 '10 at 20:12
    
Further answers for this question are given here unix.stackexchange.com/questions/78771/… –  erch Jun 9 '13 at 23:19

2 Answers 2

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

share|improve this answer
1  
...because [] represents any one of the set of characters within the brackets of which, in this case "s" is the only one... –  Dennis Williamson Sep 16 '10 at 1:53
    
yea... I really had trouble trying to explain this one in english... it's not my first language :) –  Stefan Sep 16 '10 at 8:50

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.