要想在使用grep时不显示自己,常规有下面两种做法
ps -ef | grep "nginx" |grep -v "grep"
ps -ef | grep "[n]ginx"
grep -v 过滤grep自身可以通过man获取帮助。但是grep “[n]ginx”是如何实现不显示grep本身的了?
ps -ef | grep "[n]ginx"
命令中,grep 命令的参数是正则表达式"[n]ginx"
,正则表达式"[n]ginx"
可以匹配文本"nginx"
,但是不能匹配"[n]ginx"
。
PS:我们使用grep时,也可能这样写ps -ef | grep [n]ginx
(即grep的参数不带引号),虽然也是能工作,但是man文档建议大家还是要用引号包裹参数。
man grep:
grep searches for PATTERNS in each FILE. PATTERNS is one or more patterns separated by newline characters, and grep prints each line that matches a pattern. Typically PATTERNS should be
quoted when grep is used in a shell command.
评论区