DEV Community

mojatter
mojatter

Posted on

Pipe output and getting exit status

The tee command is convenient and often used, but it does not get exit status.

#!/bin/sh
(echo 'fail' && false) | tee out.txt
Enter fullscreen mode Exit fullscreen mode

As discussed on Stack Overflow, if you are using bash, you can solve this problem by using PIPESTATUS.

I have been running into this problem often for a long time, looking for an easier way, and finally found it.

#!/bin/sh
ret=0
(echo 'fail' && false) > out.txt || ret=$?
cat out.txt && rm -rf out.txt && [ $ret ] && exit $ret
Enter fullscreen mode Exit fullscreen mode

Top comments (0)