Redirecting bash stdout/stderr to two places?

前端 未结 3 428
误落风尘
误落风尘 2021-01-30 10:35

This one\'s been bugging me for a while now. Is it possible to redirect stdout and stderr to both the terminal output and to a program?

<
3条回答
  •  攒了一身酷
    2021-01-30 11:38

    You can use a named pipe, which is intended for exactly the situation you describe.

    mkfifo some_pipe
    command_that_writes_to_stdout | tee some_pipe \
      & command_that_reads_from_stdin < some_pipe
    rm some_pipe
    

    Or, in Bash:

    command_that_writes_to_stdout | tee >(command_that_reads_from_stdin)
    

提交回复
热议问题