Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect stderr to a file for the whole pipe?

Tags:

bash

stderr

pipe

I am running a command like this:

mycmd1 | mycmd2 | mycmd3 | lp

Is there a way to redirect stderr to a file for the whole pipe instead of repeating it for each command?

That is to say, I'd rather avoid doing this:

mycmd1 2>/myfile | mycmd2 2>/myfile | mycmd3 2>/myfile | lp 2>/myfile
like image 717
springloaded Avatar asked Nov 26 '16 22:11

springloaded


1 Answers

Either

{ mycmd1 | mycmd2 | mycmd3 | lp; } 2>> logfile

or

( mycmd1 | mycmd2 | mycmd3 | lp ) 2>> logfile

will work. (The first version might be have a slightly faster (~1ms) startup time depending on the shell).

like image 83
PSkocik Avatar answered Oct 17 '22 15:10

PSkocik