Pcornel
Browse topics
Options
Recent
Favorites
Linux · Technical reference

Pipes

Direct answer

A pipe (|) connects one command's stdout to the next command's stdin, so commands run together as a pipeline.

When to use this

Use it to chain filters (grep, awk, sort), or when a pipeline fails silently.

First 30 seconds

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).

Operational verification

command 1 writes to stdout → pipe buffer → command 2 reads stdin → … → last command's exit code (or first failure with pipefail)

Evidence · next step

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
Platform / version applicability
linux
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

Platformlinux
Scope / prerequisiteUse it to chain filters (grep, awk, sort), or when a pipeline fails silently.
What next?3 suggested next steps
Evidence1
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

5 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

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 references

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.

observation · 1
Key points

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.

Key checks
  • bash -x script.sh — trace each expanded command
  • bash -n script.sh — syntax check only
  • shellcheck script.sh — common bugs and quoting problems
  • echo $? — 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 lastpipe enabled and job control inactive, the last command may run in the current shell.
  • The exit status is the status of the last command, unless pipefail is set. Then it's the status of the rightmost command that failed, or 0 if all succeeded.
  • PIPESTATUS holds the exit status of every command in the most recent pipeline.
  • |& is shorthand for 2>&1 |, so it sends standard error through the pipe too.

Common pipeline patterns

TaskPipeline
Count error linesjournalctl -u nginx -b | grep -c error
Top memory usersps -eo rss,comm --sort=-rss | head -6
Unique sources, by frequencyawk '{print $1}' access.log | sort | uniq -c | sort -rn | head
Write to a file and show itmake 2>&1 | tee build.log
Run a command per linecut -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.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

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.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

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
Technical verification history

Verification history & basis

When and against what this Topic was checked. Later text edits do not count as a new check.

2026-09-14 · vendor doclinux current

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%
Technical changelog & impact

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.

Related reference impactReview these references after material technical changes when their shared boundary is affected.
Terminology layer

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.

Aliases that resolve here
bash pipeslinux pipelinepipefailPIPESTATUS
Embedded reference data

Quick lookup tables

Pipes operational reference

Topic-specific lookup boundaries, proof limits and next-step Evidence for Pipes.

Lookup boundaryTopic-specific referenceOperational use
Scope / effective statePipes 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 verificationPin 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 chainproducer process stdout → pipe buffer/backpressure → consumer stdin → per-stage stderr + exit statuses/signals → Bash pipeline status/PIPESTATUS → downstream resultTrace requested/configured state through effective state to the observed workload result.
Failure boundaryPipeline 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 evidenceCorrelated 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 resultUse this observation only for the specific claim it actually supports.
Evidence limitIt 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 checkContinue 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 boundaryPipes
Topic pathShell & Bash → Pipes
Runtime statePipes 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 chainproducer 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 boundaryPipeline 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 ruleProve which configuration source owns the state, then prove that the running consumer loaded or generated the intended state.
Confirmation

Confirm action

Continue?