6

I've seen someone use command:

 ps -ef | grep [h]ttpd 

and Output is:

apache   25125 31006  0 21:54 ?        00:00:00 /usr/sbin/httpd
apache   26869 31006  0 22:04 ?        00:00:00 /usr/sbin/httpd
apache   27349 31006  0 22:07 ?        00:00:00 /usr/sbin/httpd
apache   27696 31006  0 22:09 ?        00:00:00 /usr/sbin/httpd
apache   28534 31006  0 22:14 ?        00:00:00 /usr/sbin/httpd
root     31006     1  0 16:16 ?        00:00:00 /usr/sbin/httpd
apache   31011 31006  0 16:16 ?        00:00:00 /usr/sbin/httpd

2 brackets surrounding the letter "h" where the grep to do?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
lotusirous
  • 3,609
  • 5
  • 17
  • 11

1 Answers1

6

It's a trick to prevent the grep command itself from appearing in the ps output.

[...] is a character class specification, i.e. [ab2] matches exactly one character that must be a, b or 2. [h] matches only exactly h.

The trick is that [h]ttp matches http, but it doesn't match itself.

Mat
  • 51,578
  • 10
  • 158
  • 140
  • Why? `It's prevent the grep command itself from appearing in the ps output.` – lotusirous Jun 09 '13 at 10:44
  • 1
    If you do `ps -ef|grep http`, the `grep` command itself will sometimes appear in the output, which is not desirable. With `[h]ttp`, the grep command filters itself out because of the above. – Mat Jun 09 '13 at 10:45
  • Regular Expression will be handled by `bash` or `grep`? – lotusirous Jun 09 '13 at 10:52
  • By grep unless you have a file that matches the `[h]ttp` glob pattern in the current directory (that would be a file called exactly `http`) - in that case, the trick will fail since the shell will expand the glob. use `grep '[h]ttp'` to prevent this. – Mat Jun 09 '13 at 10:54