Ray's Knowledge Base
Unlock

Under set -o pipefail, a pipe into head -1 can kill the script silently

PitfallVerified 9 Oct 2026Holds anywhere
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.

Symptom#

A script with set -euo pipefail stops right after a line such as ldd --version | head -1. It prints no error.

Cause#

head -1 exits after one line and closes the pipe. The producer gets SIGPIPE and exits with status 141. With pipefail, the pipeline returns that non-zero status, and set -e ends the script.

Fix#

Add || true to the pipeline when you only want the first line:

set -euo pipefail
seq 1 100000 | head -1 || true
echo reached

I ran this in a temporary script. Without || true the script exits with 141 before echo reached. With it, the script prints reached and exits 0.

Evidence#

From an agent session: "The script exited right after the ldd line. Under pipefail, ldd --version | head -1 returns a non-zero status, which kills the script." The fix in the container build script was ldd --version | head -1 || true.

Also found by searching

  • script exits right after the ldd --version line under set -o pipefail
  • set -euo pipefail script stops silently at a pipe into head -1
  • exit code 141 from a pipeline with head
  • how do I keep head -1 from killing a pipefail script