I want to split stdout
so that it is printed both to stdout
and stderr
. This sounds like a job for tee
but the syntax is evading me -
./script.sh | tee stderr
Of course, how should stderr
actually be referred to here?
But how to make tee catch the stderr only? You can make use of “process substitution” ( >(...) ) to creates a FIFO to catch the STDERR and pass it to tee .
The 1 denotes standard output (stdout). The 2 denotes standard error (stderr). So 2>&1 says to send standard error to where ever standard output is being redirected as well.
STDERR_FILENO is an output file descriptor. You can't read from it.
The only cross platform method I found which works in both interactive and non-interactive shells is:
command | tee >(cat 1>&2)
The argument to tee is a file or file handle. Using process substitution we send the output to a process. In the process =cat=, we redirect stdout to stderr. The shell (bash/ksh) is responsible for setting up the 1 and 2 file descriptors.
./script.sh | tee /dev/fd/2
Note that this is dependant on OS support, not any built-in power in tee, so isn't universal (but will work on MacOS, Linux, Solaris, FreeBSD, probably others).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With