How do I write stderr to a file while using “tee” with a pipe?

前端 未结 10 1053
独厮守ぢ
独厮守ぢ 2020-11-22 08:01

I know how to use tee to write the output (STDOUT) of aaa.sh to bbb.out, while still displaying it in the terminal:

10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 09:05

    If you're using zsh, you can use multiple redirections, so you don't even need tee:

    ./cmd 1>&1 2>&2 1>out_file 2>err_file
    

    Here you're simply redirecting each stream to itself and the target file.


    Full example

    % (echo "out"; echo "err">/dev/stderr) 1>&1 2>&2 1>/tmp/out_file 2>/tmp/err_file
    out
    err
    % cat /tmp/out_file
    out
    % cat /tmp/err_file
    err
    

    Note that this requires the MULTIOS option to be set (which is the default).

    MULTIOS

    Perform implicit tees or cats when multiple redirections are attempted (see Redirection).

提交回复
热议问题