[kwlug-disc] Question about `ps` and `grep`
Ernst Rullmann
e.rullmann at gmail.com
Thu Sep 26 14:57:50 EDT 2024
Oh that’s fun. It’s because the grep command is altered so that it can’t match itself. With [t]mux as the argument, it appears in ps aux as something like:
root 748047 0.0 0.0 9212 2560 pts/10 S+ 17:59 0:00 grep --color=auto --directories=skip [t]mux
Note the brackets. But the grep expression will only match ‘tmux’ without brackets. So it no longer finds itself.
I think you could use any other search phrase that has control characters that it doesn’t match. Some examples might be
[a-z]mux
[:alpha:]mux
t{1}mux
t+mux
I haven’t tried these yet, but I think they’d work.
Ernst
> On Sep 25, 2024, at 21:08, Ronald Barnes via kwlug-disc <kwlug-disc at kwlug.org> wrote:
>
> I watched a recent video about bash on YouTube channel "You Suck At Programming" and a comment there has me completely baffled.
>
>
> The topic was `pgrep` and `pkill` instead of `ps aux | grep ...`.
>
>
> The comment had a solution to seeing the `grep` command in the output when grep'ing `ps` output:
>
> This shows the grep command in the output:
>
>
> # ps aux | grep tmux
> root 9032 0.0 0.0 23476 5632 pts/1 S+ Sep05 0:04 sudo tmux new -s w00
> root 9041 0.0 0.0 23476 1560 pts/9 Ss Sep05 0:00 sudo tmux new -s w00
> root 9042 0.0 0.0 10916 3712 pts/9 S+ Sep05 0:00 tmux new -s w00
> root 9045 0.0 0.0 14236 4920 ? Ss Sep05 1:14 tmux new -s w00
> root 748047 0.0 0.0 9212 2560 pts/10 S+ 17:59 0:00 grep --color=auto --directories=skip tmux
>
>
>
> BUT, changing what's being grep'd for by making it [t]mux:
>
> # ps aux | grep [t]mux
> root 9032 0.0 0.0 23476 5632 pts/1 S+ Sep05 0:04 sudo tmux new -s w00
> root 9041 0.0 0.0 23476 1560 pts/9 Ss Sep05 0:00 sudo tmux new -s w00
> root 9042 0.0 0.0 10916 3712 pts/9 S+ Sep05 0:00 tmux new -s w00
> root 9045 0.0 0.0 14236 4920 ? Ss Sep05 1:14 tmux new -s w00
>
>
> Now grep's output is NOT shown.
>
>
> And I cannot figure out why not?!?
>
>
> I understand that [t] is a character range (of one character), but without explicit anchoring, why does that work?
>
>
>
> Anyone able to explain this?
>
>
> (Also, https://www.youtube.com/@yousuckatprogramming channel is highly recommended - 3 to 4 minute videos, usually about `bash` topics, where there's always something I learn.
>
> Then check the comments for user "extrageneity", who expands on the topic with a Uni CS level comment.)
>
>
> Thanks,
>
> rb
>
> _______________________________________________
> kwlug-disc mailing list
> To unsubscribe, send an email to kwlug-disc-leave at kwlug.org
> with the subject "unsubscribe", or email
> kwlug-disc-owner at kwlug.org to contact a human being.
More information about the kwlug-disc
mailing list