Search code examples
bashio-redirection

How can I redirect stderr to stdout but keep printing on stderr?


Is there a way I can "redirect" stderr to stdout but continue to print stderr?

I can redirect stderr to stdout with

cmd 2>&1 | other_cmd

However, the above will no longer print the stderr from cmd onto my terminal. I would like to redirect it and see the logs on my terminal.

Note: I do NOT want to redirect to a file, therefore I don't think tee works for me.


Edit: it was pointed out that this is similar to How do I write standard error to a file while using "tee" with a pipe?

I would say that this is a simpler subset of that problem. Though they are similar, I think this question stands on its own due to the fact that most folks (me included) do not understand the intricacies of process substitution and file redirection. In particular, folks would have some difficulty with converting:

command > >(tee -a stdout.log) 2> >(tee -a stderr.log >&2)

Into the answer given here

cmd 2> >(tee /dev/tty) | other_cmd

Among other things, that answer does not use /dev/tty, which I did not know you could do!


Solution

  • In Bash

    cmd 2> >(tee /dev/tty) | other_cmd
    

    In any shell

    { { cmd 2>&1 >&3;} | tee /dev/tty;} 3>&1 | other_cmd