Databases
cancel
Showing results for 
Search instead for 
Did you mean: 

why does this work: grep [p]md?

SOLVED
Go to solution
Todd Larchuk
Advisor

why does this work: grep [p]md?

From a question a few weeks ago I learned that
ps -ef | grep pmd might return
pmd
ovspmd
and grep pmd

but ps -ef | grep [p]md returns only
pmd
ovspmd

For the life of me I can't figure out why this works? Please enlighten me! Thanks.
5 REPLIES
harry d brown jr
Honored Contributor

Re: why does this work: grep [p]md?


Sometimes grep will show up, other times it won't. I believe it will depend upon the speed of "ps -ef" to the pipe. On my slower systems grep show's up.

live free or die
harry
Live Free or Die
S.K. Chan
Honored Contributor
Solution

Re: why does this work: grep [p]md?

Rather simple really. When you run ..
# ps -ef|grep [p]md
one of the process list will be ..

root ... grep [p]md

So running "ps -ef|grep [p]md" which specifically grep for the strings "pmd" will not return "grep pmd" string because in the process list it's "grep [p]md".

Sajid_1
Honored Contributor

Re: why does this work: grep [p]md?

hi,

I would agree with Harry, because when I tried this in my old systems (which is slow), grep show's up the result. But some case it is not. I think it really the speed at which the grep can run at the specific time.

hth
learn unix ..
Thierry Poels_1
Honored Contributor

Re: why does this work: grep [p]md?

Hi,

S.K. Chan is absolutely right!

- ps -ef | grep xxx : sometimes also lists the grep command, somtimes not; as said before probably depends on speed.

- ps -ef | grep [x]xx : cannot list the grep command itself because the command "ps -ef | grep [x]xx" will not be found by the regular expression [x]xx, as S.K. Chan mentioned.

BTW "ps -ef | grep [x]xx" is an old trick as a short replacement for "ps -ef | grep xxx | grep -v grep"

regards,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Jean-Luc Oudart
Honored Contributor

Re: why does this work: grep [p]md?