I\'m trying to write a script the essentially acts as a passthru log of all the output created by a (non-interactive) command, without affecting the output of the command to oth
A process-substitution-based solution is simple, although not as simple as you might think. My first attempt seemed like it should work
{ echo stdout; echo stderr >&2; } > >( tee ~/stdout.txt ) \
2> >( tee ~/stderr.txt )
However, it doesn't quite work as intended in bash
because the second tee
inherits its standard output from the original command (and hence it goes to the first tee
) rather than from the calling shell. It's not clear if this should be considered a bug in bash
.
It can be fixed by separating the output redirections into two separate commands:
{ { echo stdout; echo stderr >&2; } > >(tee stdout.txt ); } \
2> >(tee stderr.txt )
Update: the second tee
should actually be tee stderr.txt >&2
so that what was read from standard error is printed back onto standard error.
Now, the redirection of standard error occurs in a command which does not have its standard output redirected, so it works in the intended fashion. The outer compound command has its standard error redirected to the outer tee
, with its standard output left on the terminal. The inner compound command inherits its standard error from the outer (and so it also goes to the outer tee
, while its standard output is redirected to the inner tee
.