Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
Showing results for 
Search instead for 
Did you mean: 

why does this work: grep [p]md?

Go to solution
Todd Larchuk

why does this work: grep [p]md?

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

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

For the life of me I can't figure out why this works? Please enlighten me! Thanks.
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
Live Free or Die
S.K. Chan
Honored Contributor

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".

Honored Contributor

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


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.

learn unix ..
Thierry Poels_1
Honored Contributor

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


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"

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

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