Use it to chain filters (grep, awk, sort), or when a pipeline fails silently.
Pipes
A pipe (|) connects one command's stdout to the next command's stdin, so commands run together as a pipeline.
1. Run each stage alone and check its output.
2. set -o pipefail — does an earlier stage fail?
3. echo "${PIPESTATUS[@]}" — exit code of every stage.
4. Errors not filtered → they go to stderr; add 2>&1 if needed.
5. Variable lost after | while read → use while … done < <(cmd).
command 1 writes to stdout → pipe buffer → command 2 reads stdin → … → last command's exit code (or first failure with pipefail)
For Shell & Bash → Pipes, continue with the first unproven Topic check: Identify every stage, its stdin/stdout/stderr path and expected exit semantics. If that boundary is healthy, advance to the adjacent check: Capture PIPESTATUS or enable/use pipefail deliberately when upstream failures must affect overall success. Preserve the same host/time/identity scope and rerun the original workload before accepting recovery.
Continue only after interpreting the current observation.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 Operational evidence path Verify boundary
producer process stdout → pipe buffer/backpressure → consumer stdin → per-stage stderr + exit statuses/signals → Bash pipeline status/PIPESTATUS → downstream result
Healthy evidence: each required stage succeeds, data reaches the consumer without unintended stderr mixing/truncation, and the pipeline status policy reports failures correctly.
Step 2 Verification and output interpretation Verify boundary
Existing fast checks for this Topic. - Identify every stage, its stdin/stdout/stderr path and expected exit semantics.
- Capture PIPESTATUS or enable/use pipefail deliberately when upstream failures must affect overall success.
- Test empty input, large input/backpressure and early-consumer-exit behavior where relevant.
- Verify the final consumer result and any target state, not just displayed pipeline text.
Verification chain. producer process stdout → pipe buffer/backpressure → consumer stdin → per-stage stderr + exit statuses/signals → Bash pipeline status/PIPESTATUS → downstream result
Step 3 Commands Verify boundary
ps aux | grep '[n]ginx'
set -o pipefail
Step 4 Failure modes and next checks Narrow the fault
Suspicious evidence: an upstream command fails silently, stderr bypasses the pipeline, early exit triggers SIGPIPE unexpectedly, or buffering masks a stalled stage.
Step 5 Common mistakes Narrow the fault
- Ignoring pipeline exit status.
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.
The pipeline's exit status is the last command's, so earlier failures are hidden — use set -o pipefail. stderr is not piped unless you add 2>&1 (or |&). Commands in a pipeline run in subshells, so variables set there are lost.
bash -x script.sh— trace each expanded commandbash -n script.sh— syntax check onlyshellcheck script.sh— common bugs and quoting problemsecho $?— exit code of the last command
ReferenceWhat it is1 section · depth on demand
Core model
A pipeline is not a sequential text macro. Processes usually run concurrently, each stage has its own status, stderr is separate unless redirected, buffering/backpressure can affect timing, and Bash returns the last command status unless pipefail or PIPESTATUS is used.
ReferenceHow it works1 section · depth on demand
How it works
How it works: A Linux flow is scoped to a network namespace and crosses socket/source selection, routing policy and FIB lookup, neighbor resolution, netfilter/eBPF/qdisc where configured, device/bridge/bond/VLAN layers and the physical/virtual link.
ReferenceImportant terms & boundaries5 sections · depth on demand
What this shows and what it does not
Pipeline verification requires both data correctness and the required stage-status policy; the last command's zero status alone is not enough.
In depth
Pipelines and their exit status
A pipeline connects the output of one command to the input of the next. The bash(1) manual page describes how pipelines behave:
- Each command in a multi-command pipeline runs in its own subshell. With
lastpipeenabled and job control inactive, the last command may run in the current shell. - The exit status is the status of the last command, unless
pipefailis set. Then it's the status of the rightmost command that failed, or 0 if all succeeded. PIPESTATUSholds the exit status of every command in the most recent pipeline.|&is shorthand for2>&1 |, so it sends standard error through the pipe too.
Common pipeline patterns
| Task | Pipeline |
|---|---|
| Count error lines | journalctl -u nginx -b | grep -c error |
| Top memory users | ps -eo rss,comm --sort=-rss | head -6 |
| Unique sources, by frequency | awk '{print $1}' access.log | sort | uniq -c | sort -rn | head |
| Write to a file and show it | make 2>&1 | tee build.log |
| Run a command per line | cut -d: -f1 /etc/passwd | xargs -n1 id |
The subshell pitfall
Because each part of a pipeline runs in a subshell, variables set inside a while read loop at the end of a pipe are lost when the pipe ends. Use process substitution (done < <(cmd)) or shopt -s lastpipe in scripts.
host$ false | true; echo "status=$?"
status=0
host$ set -o pipefail; false | true; echo "status=$?"; set +o pipefail
status=1
host$ grep -h ERROR /var/log/app/*.log | sort | uniq -c | sort -rn | head -3; echo "${PIPESTATUS[@]}"
42 ERROR db timeout
7 ERROR cache miss storm
0 0 0 0 0
host$ count=0; printf 'a\nb\n' | while read -r l; do count=$((count+1)); done; echo $count
0
host$ count=0; while read -r l; do count=$((count+1)); done < <(printf 'a\nb\n'); echo $count
2
pipefail in scripts
Without pipefail, curl ... | tar xz reports success when the download fails, as long as tar exits 0. set -o pipefail at the top of a script makes such failures visible. Check PIPESTATUS when you need to know which part failed.
Documented example, not captured output.
How it works at runtime
Pipes is part of Shell & Bash → Pipes. Pipes connect one process' stdout to another process' stdin, creating concurrent producer/consumer stages whose buffering, lifecycle and individual exit statuses must be evaluated separately.
Topic-specific state model. Pipes connect one process' stdout to another process' stdin, creating concurrent producer/consumer stages whose buffering, lifecycle and individual exit statuses must be evaluated separately.
Interpretation rule. - Pipeline output looks correct ≠ every upstream stage succeeded.
- stdout is piped ≠ stderr is piped.
- Producer completion ≠ consumer processed all data successfully.
- SIGPIPE/early consumer exit ≠ necessarily an application bug; it can be expected in bounded readers such as head.
Quick reference
Pipes connect stdout of one process to stdin of the next; all run concurrently.
ps aux | grep '[n]ginx' # bracket trick avoids matching grep itself
du -sh /var/* 2>/dev/null | sort -h | tail
cmd 2>&1 | tee -a run.log # stdout+stderr to screen and file
set -o pipefail; false | true; echo $? # 1 with pipefail
echo "${PIPESTATUS[@]}" # exit code of each part
A pipe's exit status is the last command's unless pipefail is set — scripts that check $? after a pipeline miss failures otherwise.
Limits and how it fails
Primary failure point. Pipeline verification requires both data correctness and the required stage-status policy; the last command's zero status alone is not enough.
ReferenceConfiguration & state1 section · depth on demand
Configuration, persistence and lifecycle
Distribution/platform context. Redirection owns fd routing to/from files/devices; stdin/stdout/stderr owns descriptor roles; this Topic owns inter-process pipeline composition and status.
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 Shell & Bash → Pipes. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://www.gnu.org/software/bash/manual/bash.html · confidence 94%What changed and what may be affected
Recent technical edits are separated from ordinary page timestamps. Relationship impact is advisory: it identifies nearby canonical references to review, not automatic breakage.
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.
Quick lookup tables
Pipes operational reference
Topic-specific lookup boundaries, proof limits and next-step Evidence for Pipes.
| Lookup boundary | Topic-specific reference | Operational use |
|---|---|---|
| Scope / effective state | Pipes connect one process' stdout to another process' stdin, creating concurrent producer/consumer stages whose buffering, lifecycle and individual exit statuses must be evaluated separately. | Establish exactly what this Topic owns before crossing into another subsystem. |
| Fast verification | Pin host/distribution + shell/version/mode → identify cwd/mount namespace + input/path/stream → preserve quoting/expansion and final argv → map fd/pipeline/process boundaries → inspect object/variable/status/signal state → make one minimal reversible change → read back effective state → repeat original consumer/script/workload test. | Use the shortest evidence path first; a fast check is a boundary test, not blanket proof. |
| Verification chain | producer process stdout → pipe buffer/backpressure → consumer stdin → per-stage stderr + exit statuses/signals → Bash pipeline status/PIPESTATUS → downstream result | Trace requested/configured state through effective state to the observed workload result. |
| Failure boundary | Pipeline verification requires both data correctness and the required stage-status policy; the last command's zero status alone is not enough. | Stop broad troubleshooting at the first boundary that is not supported by evidence. |
| Positive evidence | Correlated Linux file/text/shell evidence can establish this proof path: producer process stdout → pipe buffer/backpressure → consumer stdin → per-stage stderr + exit statuses/signals → Bash pipeline status/PIPESTATUS → downstream result | Use this observation only for the specific claim it actually supports. |
| Evidence limit | It does not independently prove behavior outside the captured host, shell/session, cwd/namespace, input, process, filesystem, locale, time or workload scope. Pipeline verification requires both data correctness and the required stage-status policy; the last command's zero status alone is not enough. | Do not promote a local success signal into end-to-end or adjacent-dependency proof. |
| Next troubleshooting check | Continue from the first unproven boundary: capture the exact shell-delivered argv/streams or resolved object, verify the effective target state, change only the demonstrated cause, then repeat the original consumer/script/workload and adjacent regression checks. | Continue to the next unresolved boundary while preserving the original test condition. |
Pipes · operational reference
Compact Linux state, persistence, verification and boundary reference for fast lookup.
| Reference boundary | Pipes |
|---|---|
| Topic path | Shell & Bash → Pipes |
| Runtime state | Pipes connect one process' stdout to another process' stdin, creating concurrent producer/consumer stages whose buffering, lifecycle and individual exit statuses must be evaluated separately. |
| Fast checks | - Identify every stage, its stdin/stdout/stderr path and expected exit semantics. - Capture PIPESTATUS or enable/use pipefail deliberately when upstream failures must affect overall success. - Test empty input, large input/backpressure and early-consumer-exit behavior where relevant. - Verify the final consumer result and any target state, not just displayed pipeline text. |
| Verification chain | producer process stdout → pipe buffer/backpressure → consumer stdin → per-stage stderr + exit statuses/signals → Bash pipeline status/PIPESTATUS → downstream result Boundary chain: local authoritative state → adjacent dependency → effective/programmed state → original workload/request → return/regression → explicit Evidence boundary. Boundary chain: local authoritative state → adjacent dependency → effective/programmed state → original workload/request → return path or regression → explicit Evidence boundary. |
| Failure boundary | Pipeline verification requires both data correctness and the required stage-status policy; the last command's zero status alone is not enough. Boundary chain: process/socket namespace → source/rule/FIB → neighbor → netfilter/eBPF/qdisc → virtual/physical device → peer → return → application. |
| Persistence rule | Prove which configuration source owns the state, then prove that the running consumer loaded or generated the intended state. |