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)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay