Broken pipe (SIGPIPE)
Exit code 141 is caused by the SIGPIPE signal (signal 13). It occurs when a command tries to write to a pipe, but the command on the reading end has already terminated. The exit code is 128 + 13.
- 1In a pipeline `cmd1 | cmd2`, `cmd2` terminates before `cmd1` has finished writing all its output to the pipe.
Piping the output of a command to `head`, which closes the pipe after reading the first few lines.
#!/bin/bash
# 'yes' produces output forever. 'head' takes 1 line and exits.
# 'yes' receives SIGPIPE when it tries to write to the closed pipe.
yes | head -n 1
# Note: The exit code of a pipeline is the exit code of the last command.
# To see the exit code of 'yes', we check PIPESTATUS.
echo "Exit of head: $?"
echo "Exit of yes: ${PIPESTATUS[0]}"expected output
y Exit of head: 0 Exit of yes: 141
Fix
Acknowledge that this is often normal behavior
WHEN Using commands like `head`, `less`, or `grep -m`
set -o pipefail # Now the pipeline will fail if 'yes' fails yes | head -n 1 || echo "Pipeline failed"
Why this works
In many cases, SIGPIPE is expected and not an error. If you need to ensure all parts of a pipeline succeed, `set -o pipefail` makes the pipeline's exit status reflect the first command to fail, rather than the last.
✕ Treat it as a critical error without context
This is a very common and often intentional outcome in shell pipelines.
GNU Bash Manual
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev