The tee command is convenient and often used, but it does not get exit status.
#!/bin/sh
(echo 'fail' && false) | tee out.txt
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
Top comments (0)