Use this when chaining commands with pipes.
Pipes
Pipes in Bash send one command's output to the next, such as journalctl -u nginx | grep error | wc -l. Use set -o pipefail so a failure anywhere in the pipe is detected.
1. Pipes connect stdout to stdin.
2. pipefail makes pipeline fail if any command fails.
3. PIPESTATUS for details.
4. Pipes run in subshells.
5. Keep pipelines readable.
cmd1 stdout → cmd2 stdin
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Verification path Verify boundary
The negative fixture should deliberately exercise risks such as unquoted expansion, globbing, pipeline status loss, subshell scope, set -e misconceptions, signal races, temporary-file leakage and destructive path ambiguity, choosing the cases most relevant to producer/consumer process boundaries, buffering, exit status for each command, stderr separation and avoiding silent failure of non-final pipeline commands.
Step 2 Common mistakes Narrow the fault
- Missing
pipefail, so failed pipelines return 0. - Useless
cat file | grep.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- set -o pipefail makes a failure anywhere fail the pipe.
- Each pipe stage runs in a subshell.
- Variables set inside a pipe do not survive it.
set -o pipefail— pipeline failures detectedcmd1 | cmd2; echo "${PIPESTATUS[@]}"— per-command status- Avoid useless cat — efficiency
ReferenceWhy it matters1 section · depth on demand
What it is
A pipe sends one command's output to the next command's input. Use set -o pipefail so a failure early in the pipe is not hidden by the last command's success.
ReferenceImportant terms & boundaries2 sections · depth on demand
In depth
Pipes connect commands so the output of one becomes the input of the next — the core of shell automation. Small tools (grep, awk, sort, jq) combine into powerful one-liners.
# Top 10 source IPs denied in a firewall log
grep ' DENY ' fw.log | awk '{print $5}' | sort | uniq -c | sort -rn | head -10
# Device names from an API
curl -s "$NB/api/dcim/devices/?site=oslo" -H "Authorization: Token $T" | jq -r '.results[].name'
By default a pipeline's exit status is that of the last command, so failures earlier in the pipe are hidden. set -o pipefail makes the pipeline fail if any command fails; ${PIPESTATUS[@]} shows each command's status.
Example
set -o pipefail
journalctl -u nginx --since today | grep -c ' 500 '
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Bash Automation > Pipes. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://www.gnu.org/software/bash/manual/bash.html · confidence 94%Deep reference
Healthy vs suspicious evidence
Output interpretation
For producer/consumer process boundaries, buffering, exit status for each command, stderr separation and avoiding silent failure of non-final pipeline commands, prefer typed fields and stable IDs over human presentation text.