On 9/18/25 4:45 AM, Alexis wrote:

> However, the command line i provided in my post was:
> 
>  # ps ax | grep X
> 
> where the `grep` is GNU grep, and will match _any_ instance of the
> character 'X' in the process list, not just a process that's literally
> nothing more than 'X'. That's why it listed Xwayland and the grep
> process itself in the output:
> 
>  3576 tty1     Sl+    0:00 Xwayland :0 -rootless -core -terminate  -
> listenfd 26 -listenfd 27 -displayfd 67 -wm 64
>  12043 pts/21   S+     0:00 grep --color=auto X
> 
> It would have also listed Xnest, had it been running.
> 
> That's how Basic Regular Expressions (BREs), as used by default by GNU
> grep, work. If one wanted to match a literal 'X' and nothing more, one
> would use the -F option to GNU grep, or fgrep, to indicate that one
> doesn't want the pattern to be interpreted as an RE, but as a fixed string.


Well, no.

$ ps ax | grep -F bas
[...]
 753347 pts/6    Ss+    0:00 /bin/bash
1269600 pts/7    Ss     0:01 /bin/bash
1770581 pts/4    Ss     0:09 /bin/bash
1992403 pts/8    Ss     0:01 /bin/bash
[...]


Fixed strings != "nothing more", ironically you tend to need regex for
word anchors like \b


$ ps ax | grep '\bbas\b'
$ ps ax | grep '\bbash\b'
[...]
1269600 pts/7    Ss     0:01 /bin/bash
1770581 pts/4    Ss     0:09 /bin/bash
1992403 pts/8    Ss     0:01 /bin/bash
2226387 pts/9    Ss+    0:25 /bin/bash
[...]


Personally I recommend

$ pgrep -af '\bbash\b'
1393 -bash
1788 /bin/bash
1962 -bash
559140 bash
753347 /bin/bash
1269600 /bin/bash
1770581 /bin/bash
1992403 /bin/bash
[...]


pgrep won't catch the pipe to grep :) it also has -f, whose lack allows
matching only inside the invoking exe name.


-- 
Eli Schwartz

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature

Reply via email to