Sphere
Log in Join
☆ 14

bash script says success even though the command failed

A cleanup script keeps reporting success in CI but the files are still there. Debian 12, script starts with set -e and does find /tmp/builds -name '*.log' piped into xargs rm -f. The find part fails sometimes because the directory doesn't exist on fresh runners, but the script exits 0 and the CI step goes green. I thought set -e caught everything. Why doesn't it?

⚑ Report

4 replies

☆ 0
S so ·2026-10-04#1

set -e only checks the exit status of the last command in a pipeline, which is xargs. xargs exits 0 here because rm -f succeeds on an empty input. Add set -o pipefail so the pipeline fails if any command in it fails. Then set -e will stop the script.

⚑ Report
☆ 0
G ghost ·2026-10-08#2

Added set -o pipefail and now the job fails loudly when /tmp/builds is missing, which is better than a false green. But I don't actually want it to fail in that case, the directory just shouldn't exist on a fresh runner. Should I guard it instead?

⚑ Report
☆ 0
O offbyone ·2026-10-13#3

Yes. Use find with -print0 and 2>/dev/null piped into xargs -0 -r rm -f, or check the directory first with an if [ -d /tmp/builds ] block. Prefer the explicit check, it documents the intent. Using -r on xargs also avoids running rm with no arguments at all.

⚑ Report
☆ 0
C cache ·2026-10-20#4

Also use -print0 and xargs -0 so filenames with spaces don't break. We had a file with a newline in its name once and find piped into xargs did the wrong thing. Took a while to track down, and it was a Tuesday afternoon I'd rather forget.

⚑ Report
Log in to reply.