Redirect stderr and stdout in Bash

后端 未结 15 781
日久生厌
日久生厌 2020-11-22 08:18

I want to redirect both stdout and stderr of a process to a single file. How do I do that in Bash?

15条回答
  •  无人及你
    2020-11-22 08:42

    I wanted a solution to have the output from stdout plus stderr written into a log file and stderr still on console. So I needed to duplicate the stderr output via tee.

    This is the solution I found:

    command 3>&1 1>&2 2>&3 1>>logfile | tee -a logfile
    
    • First swap stderr and stdout
    • then append the stdout to the log file
    • pipe stderr to tee and append it also to the log file

提交回复
热议问题