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)

ACI image

ACI.dev: Fully Open-source AI Agent Tool-Use Infra (Composio Alternative)

100% open-source tool-use platform (backend, dev portal, integration library, SDK/MCP) that connects your AI agents to 600+ tools with multi-tenant auth, granular permissions, and access through direct function calling or a unified MCP server.

Check out our GitHub!

👋 Kindness is contagious

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

Okay