How to make tee catch the stderr only in Linux?

I would like to make tee catch the stderr and log it into a file.

A brute force method by let tee listen on both stderr and stdout is okay like

cmd 2>&1 | tee -a log

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.

Lets take an example t.sh:

#!/bin/bash

echo hello
1>&2 echo world

It works as expected:

$ ./t.sh 
hello
world
$ ./t.sh >/dev/null
world
$ ./t.sh 2>/dev/null
hello

Now, let’s redirect the STDERR to tee:

$ ./t.sh 2> >(tee /tmp/log)
hello
$ world
cat /tmp/log 
world

tee prints to STDOUT by default. Let’s make it print to STDERR:

$ ./t.sh 2> >(tee /tmp/log >&2)
hello
world
$ cat /tmp/log 
world

Verify it:

$ (./t.sh 2> >(tee /tmp/log >&2) ) >/dev/null
world

The “world” is not to the STDOUT anymore.

Answered by Eric Z Ma.

Eric Z Ma

Eric is a father and systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

Your email address will not be published. Required fields are marked *