How to redirect STDERR to both file and console and STDOUT to file only


What I want to achieve is redirect both normal message and error message to a file. But also print the error message to the console (only error message).

Best Answer

  • One (out of many) solutions is the following:

    command 2>&1 1>logfile | tee -a logfile


    `2>&1` redirect the output of STDERR to STDOUT
    `1>logfile` redirect STDOUT (note: leaves STDERR unchanged)
    `| tee -a logfile` append the redirected STDERR to the logfile

    For more information, see the Bash Hackers Wiki

  • Related Question