Bash and pipefail – Non deterministic conditionals
Comments
Ranked by a public rubric, not by votes: what a comment adds counts, not who wrote it. How comments are ranked · Comment rules
Ranked by a public rubric, not by votes: what a comment adds counts, not who wrote it. How comments are ranked · Comment rules
Summary
A debugging write-up explains why conditions like long-output-cmd | grep -q magic fail intermittently in scripts using set -eo pipefail. Once grep finds a match it exits, and the still-writing producer is killed by SIGPIPE (status 141), which pipefail then reports. Small outputs fit in the 64 KB pipe buffer and never fail; larger ones fail more often under CPU load. The author's tests on one machine show head -1, sed 1q and grep -m1 behave the same. Fixes: read to EOF, capture output first, or tolerate 141.