Search code examples
bashpipestdoutstderr

Bash: how do I pipe stdout and stderr of one process to two different processes?


I have a process myProcess1 that produces both stdout and stderr outputs. I want to pipe the two output streams into two different downstream processes, myProcess2 and myProcess3, that will massage the data and then dump the results into two different files. Is it possible to do it with a single command? If not, the 2nd best would be running two separate commands, one to process stdout, the other stderr. In this case, the first run would simply be:

myProcess1 | myProcess2 > results-out.txt

What would be a similar command to process stderr? Thx


Solution

  • Without fancy games something like this should work:

    { myProcess1 | myProcess2 > results-out.txt; } 2>&1 | myprocess3 > results-err.txt
    

    With fancy games (which do not work in /bin/sh, etc.) you could do something like this:

    myProcess1 2> >(myprocess3 > results-err.txt) | myProcess2 > results-out.txt