head: Usage, Options & Common CI Errors
head shows the beginning of a file or stream - the first 10 lines by default.
head is a trivial command with one non-trivial CI interaction: when it closes a pipe early, the upstream process gets SIGPIPE, which with set -o pipefail can fail the step.
What it does
head outputs the first part of each input - by default the first 10 lines, or a chosen number of lines (-n) or bytes (-c). It is commonly used to preview files or cap output.
Common usage
head file.txt # first 10 lines
head -n 5 file.txt
head -n -3 file.txt # all but the last 3 (GNU)
head -c 100 file.bin # first 100 bytes
some_command | head -n 20Options
| Flag | What it does |
|---|---|
| -n <N> | First N lines (-N also works) |
| -n -<N> | All but the last N lines (GNU) |
| -c <N> | First N bytes |
| -q / -v | Never / always print file headers |
Common errors in CI
When head exits after N lines it closes the pipe, sending SIGPIPE to the producer; that producer exits 141 (128+13). With set -o pipefail, cmd | head can therefore make the whole pipeline "fail" even though it worked - handle it deliberately (e.g. cmd | head || true, or restructure). Negative counts (head -n -3) and -c byte mode are GNU features absent on some BSD/BusyBox builds.