-1

I am trying to use

 ps -C chromi* 

to see all chromium processes, but no success. How can I use regular expression in here?

  • I'd use `ps -ef | grep 'chromi*'`, although there might be a solution that only relies on `ps`. In the context of your current command, `*` isn't interpreted as a regex quantifier but as part of a glob, trying to match files in your current working directory that would start with "chromi" – Aaron Oct 11 '21 at 09:16
  • @Aaron, thanks. I know it works, but I want to use regular expression in shell(i.e. bash) commands directly. Do you know where do I have to look? I could not find direct usage of RE except some commands like grep, sed. etc. –  Oct 11 '21 at 09:25
  • In terms of pure bash handling regex, I don't think you have anything else than test's `=~` operator. So you could loop over the lines returned by `ps` and use `if [ "$ps_line" =~ *chromi* ]; then ...`, but I don't see any reason to bother with such verbose code – Aaron Oct 11 '21 at 09:44
  • I'm mentionning `test`/`[` because it's a builtin, if you mean any command then it depends on the command, and the `grep`-based solution I provided above was a perfectly acceptable bash command relying on regex. The `pgrep` solution that was posted is probably exactly what you want (a way to filter processes based on a regex using a single command), but I wanted to clear some misconceptions. There's no generic "filter my output based on regex" option for bash commands – Aaron Oct 11 '21 at 12:42

2 Answers2

1

How to use "Regular Expression" in ps?

You cannot, ps does not support regular expressions. The argument is parsed literally.

How to use "Regular Expression" in ps?

You can patch procps ps to support it, most probably (with yet another!) additional flag. The patch looks simple, basically another tree traversing parse_* function that uses regex.h instead of strncmp.

I doubt such patch would make it upstream - it's typical to use other tools, most notably pgrep or shell with a pipe and grep, to filter process by command line name. ps has to stay POSIX compatible, and has so many options already.

Note that regular expression is not "globbing". Consult man 7 glob vs man 7 regex. Regular expression chromi* would match chrom or chromiiiii - chrom followed by zero or more i.

Note that unquoted arguments with "trigger" characters undergo filename expansion (ls 'chromi*' vs ls chromi*). This is different than passing the literal argument when there exist files that match the pattern. If the intention is to pass the pattern to the tool, quote the argument to prevent filename expansion.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Thanks for the info of patching, this is the answer I am looking for, not the alternative commands. –  Oct 16 '21 at 23:30
1

I think you are looking for pgrep:

pgrep -f chromium

This will print pids only, no further information.

With the help of xargs, this can be piped to ps again for detailed output:

pgrep -f chromium | xargs ps -o pid,cmd,user,etime -p
hek2mgl
  • 152,036
  • 28
  • 249
  • 266