Pipe output and capture exit status in Bash

后端 未结 15 1391
盖世英雄少女心
盖世英雄少女心 2020-11-22 08:07

I want to execute a long running command in Bash, and both capture its exit status, and tee its output.

So I do this:

command | tee out.txt
ST=$?
         


        
15条回答
  •  猫巷女王i
    2020-11-22 08:33

    Outside of bash, you can do:

    bash -o pipefail  -c "command1 | tee output"
    

    This is useful for example in ninja scripts where the shell is expected to be /bin/sh.

提交回复
热议问题