bashio-redirectiontee

capture only stderr from command


I want to run a command. I want to continue to pipe stdout and stderr to stdout/stderr of the console, but I also want to capture stderr in a variable.

To do this with stdout is trivial stdout=$(command | tee), but I can't see any way to do this for stderr.


Solution

  • Just swap stdout and stderr before using the same practice you already know.

    stderr=$(yourcommand 3>&1 >&2 2>&3 3>&- | tee /dev/fd/2)
    

    Breaking that down:

    ...and then tee /dev/fd/2 makes a copy of new-stdout (original-stderr) on original-stderr so there's a copy of content on the terminal, uncaptured.


    Thus, these four redirections in order have the effect of swapping stdout and stderr for the duration of the capture, and logging a copy of what was originally stdout for human consumption via stderr.