2015-06-19 110 views
0
# ps | grep safe 
14592 root  136m S /tmp/data/safe/safe 
16210 root  1664 S grep safe 

# ps | grep safe\$ 
14592 root  258m S /tmp/data/safe/safe 

那么\$是什么意思?这是一个正则表达式吗?

+0

感谢您的详细答案。从我理解的bash $意味着字符串以bash结尾,是吗? – GoodightE

回答

1

是的,这是一个正则表达式。 $是一个正则表达式字符,意思是“行尾”。所以通过说grep safe\$你是grep ping所有名字以safe结尾的行并且避免grep本身成为输出的一部分。

这里要说的是,如果你运行ps命令和grep其输出的grep本身将被列出的事情:

$ ps -ef | grep bash 
me  30683 9114 0 10:25 pts/5 00:00:00 bash 
me  30722 8859 0 10:33 pts/3 00:00:00 grep bash 

所以说grep safe\$,或等值grep "safe$",要添加一个正则表达式在比赛,这将使grep本身不显示。

$ ps -ef | grep "bash$" 
me  30683 9114 0 10:25 pts/5 00:00:00 bash 

作为一个有趣的情况,如果您使用grep-F选项,它会精确匹配字符串,所以你会得到唯一的输出是grep本身:

$ ps -ef | grep -F "bash$" 
me  30722 8859 0 10:33 pts/3 00:00:00 grep -F bash$ 

的典型伎俩这是grep -v grep,但你可以在More elegant "ps aux | grep -v grep"找到其他人。我喜欢那个说ps -ef | grep "[b]ash"的那个。

相关问题