How to redirect output to a file and stdout

后端 未结 10 1364
借酒劲吻你
借酒劲吻你 2020-11-22 08:11

In bash, calling foo would display any output from that command on the stdout.

Calling foo > output would redirect any output from that

10条回答
  •  死守一世寂寞
    2020-11-22 08:49

    The command you want is named tee:

    foo | tee output.file
    

    For example, if you only care about stdout:

    ls -a | tee output.file
    

    If you want to include stderr, do:

    program [arguments...] 2>&1 | tee outfile
    

    2>&1 redirects channel 2 (stderr/standard error) into channel 1 (stdout/standard output), such that both is written as stdout. It is also directed to the given output file as of the tee command.

    Furthermore, if you want to append to the log file, use tee -a as:

    program [arguments...] 2>&1 | tee -a outfile
    

提交回复
热议问题