Why does enclosing a single char in brackets in a regex exclude the grep itself when grepping ps?

前端 未结 2 1157
梦谈多话
梦谈多话 2021-01-19 19:17

If I perform the following grep on my Linux box:

$ ps -ef | grep bash
root      2286     1  0 Jun06 ?        00:03:15 /bin/bash /etc/init.d/zxy100wd
wmiller          


        
2条回答
  •  耶瑟儿~
    2021-01-19 19:39

    This is because the expression ba[s]h (or [b]ash, or...) just matches bash, not ba[s]h (or [b]ash, or...).

    So the grep command is looking for all lines with bash:

    root      2286     1  0 Jun06 ?        00:03:15 /bin/bash /etc/init.d/zxy100wd
    wmiller   6436  6429  0 Jun06 pts/0    00:00:01 bash
    wmiller  10707  6429  0 Jun07 pts/1    00:00:00 bash
    wmiller  10795  6429  0 Jun07 pts/2    00:00:00 bash
    

    but

    wmiller  16220  6436  0 06:55 pts/0    00:00:00 grep --color=auto ba[s]h
    

    does not match because it is not exactly bash.

提交回复
热议问题