Tee to commands only, not stdout

Asked by At

I already know how to use tee with process substitution to send output to various commands, and stdout, eg

command0 | tee >(command1) >(command2)

With the above line, stdout will be composed of interleaved lines from command0, command1, and command2.

Is there a way to prevent tee from writing to stdout, without removing the output of any commands it pipes to? So for the example above, for stdout to only have output from command1 and command2? Most answers relating to teeing without stdout are only writing directly to files, and recommend using something like this:

command0 | tee file1 file2 >/dev/null

But with process substitution, that would consume all output from the other commands too.

command0 | tee >(command1) >(command2) >/dev/null

Is there some way to tell tee not to print to stdout, or to only consume the output directly from tee?

2 Answers

Zoey Hewll On

I have seen a comment and an answer that use an extra >, but don't really explain why it does what it does. It seems like it is redirecting output somewhere but all I can tell so far is that it does what I'm looking for. This works:

command0 | tee > >(command1) >(command2)
command0 | tee >(command1) > >(command2)

it appears not to matter where the extra > is, so long as it is before at least one of the arguments to tee. So this will not work:

command0 | tee >(command1) >(command2) >

Without knowing what this is called, and with no further leads, I can't explain further.

Wiimm On

Try this:

( command0 | tee >(command1 1>&3 ) | command2 ) 3>&1

It redirects the stdout of command1 to pipe 3, so that command2 sees only the original source. At end, you redirect pipe 3 to stdout again.

Use this to test it:

( echo test | tee >( sed 's/^/1 /' >&3 )  | sed 's/^/2 /' ) 3>&1

The output is unordered and in my case:

2 test
1 test