Pipe output and capture exit status in Bash

后端 未结 15 1096
盖世英雄少女心
盖世英雄少女心 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条回答
  •  终归单人心
    2020-11-22 08:33

    (command | tee out.txt; exit ${PIPESTATUS[0]})
    

    Unlike @cODAR's answer this returns the original exit code of the first command and not only 0 for success and 127 for failure. But as @Chaoran pointed out you can just call ${PIPESTATUS[0]}. It is important however that all is put into brackets.

提交回复
热议问题