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

天大地大妈咪最大 提交于 2019-12-20 02:29:37

问题


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:


The pattern [f]irefox will not match the literal string [f]irefox. Instead it will match strings with exactly one char from the 1-character class [f], followed by irefox.



来源:https://stackoverflow.com/questions/20528894/how-does-ps-aux-grep-pattern-exclude-grep-itself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!