Why my strace command is not working with fish?

Viewed 108

I'm trying to launch a command that I'm able to launch with Bash but not with Fish :

  • On Bash this is working well :
$ sudo strace -f -s3000 -p $(pgrep -f teams -d " -p ") -o /tmp/debug.log
strace: Process 49774 attached with 32 threads
strace: Process 49778 attached
strace: Process 49780 attached
strace: Process 49817 attached with 10 threads
strace: Process 49824 attached with 6 threads
strace: Process 49848 attached with 13 threads
strace: Process 55055 attached with 6 threads
strace: Process 56914 attached with 17 threads
strace: Process 56965 attached with 44 threads
strace: Process 57041 attached with 10 threads
  • On Fish i'm trying the same but without the '$' but I got an error :
$ sudo strace -f -s3000 -p (pgrep -f teams -d " -p ") -o /tmp/debug.log
strace: Invalid process id: '-p'

This strace is to follow all the processes of Teams, here is the pgrep command only (for info) :

$ pgrep -f teams -d " -p "
49774 -p 49778 -p 49780 -p 49817 -p 49824 -p 49848 -p 55055 -p 56914 -p 56965 -p 57041

Any idea ?

3 Answers

Unlike bash, fish splits command substitutions on newlines only, not spaces or tabs or anything else.

This means when your 9774 -p 49778 -p 49780 is passed to strace as one argument, where you want it to be five - 9774, -p, 49778, -p, 49780.

The solution is to just split it explicitly with string split:

sudo strace -f -s3000 -p (pgrep -f teams -d " -p " | string split " ") -o /tmp/debug.log

You could build up the command options in pieces:

set opts -f -s3000 -o /tmp/file
for pid in (pgrep -f team); set opts $opts -p $pid; end

sudo strace $opts

This takes advantage of the list nature of fish variables.

First, set pgrep in a variable and do a string split with spaces

$ set PROCS (string split ' ' (pgrep -f teams -d " -p")) 

Then call it with :

$ sudo strace -f -s3000 -p $PROCS -o /tmp/debug.log 
Related