How does ps aux | grep '[p]attern' exclude grep itself?

Viewed 2478

The title says it all. I've seen this idiom used alot instead of adding an additional grep -v grep in some ps pipeline. For example it could be used like this:

$ ps aux | grep '[f]irefox' | awk '{ print $8 }'

instead of

$ ps aux | grep 'firefox' | grep -v grep | awk '{ print $8 }'

It's super-convenient, but how does it work and why?

1 Answers
Related