Skip to main content

Pipe

A pipe (|) connects the standard output of one command to the standard input of the next, so commands can be chained without temporary files. The kernel implements it as an in-memory buffer between the processes.

Understanding Linux Pipes

What Is a Pipe in Linux?

A pipe is a conveyor belt between commands. Output from the command on the left flows straight into the command on the right. In ps aux | grep nginx | awk '{print $2}', ps lists processes, grep keeps the nginx lines, and awk prints the PID column, with no files in between.

How Pipes Run Commands in Parallel

All commands in a pipeline start at the same time. The kernel keeps a small buffer (commonly 64 KB on Linux) between each pair. If the reader is slow the writer blocks until there is room, and the reader can start work before the writer finishes.

flowchart LR A[ps aux] -->|stdout to stdin| B[grep nginx] B -->|stdout to stdin| C[awk print PID] C --> D([Terminal])

Practical Pipe Patterns

Bash
# Top 10 client IPs for HTTP 200 responses (field 9 is status in common log format)
awk '$9 == 200 {print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -10
# Most common error messages
grep 'ERROR' /var/log/app.log | sort | uniq -c | sort -rn | head -5
# Parse JSON from an API
curl -s https://api.internal/health | jq '.services[] | select(.status == "down")'
# Save and filter at once
./deploy.sh 2>&1 | tee deploy.log | grep ERROR
# Named pipe (FIFO) that persists as a file
mkfifo /tmp/mypipe
echo "data" > /tmp/mypipe &
cat /tmp/mypipe
rm /tmp/mypipe

How to Check Exit Codes in a Pipeline

By default a pipeline's exit status is the last command's status, so earlier failures are hidden.

Bash
false | true ; echo $? # 0, the failure is masked
set -o pipefail
false | true ; echo $? # 1, now it surfaces
grep pattern file | wc -l
echo "${PIPESTATUS[@]}" # exit code of each stage

Troubleshooting Pipes

Symptom Command What to Check
Pipeline always "succeeds" echo ${PIPESTATUS[@]} Earlier stage failed
Missing error messages Add 2>&1 before the pipe stderr is not piped
echo cmd | sudo bash oddities Run command directly Sudo and stdin interplay
Tip

Use tee to keep a full copy while filtering: command | tee full.log | grep ERROR.

Common Mistake

Expecting a pipe to carry error output. Only stdout flows through |. Add 2>&1 (or use |& in bash) to include stderr.

Remember

Use set -o pipefail in production scripts so a failing stage fails the whole pipeline.

Frequently Asked Questions

Why are pipes faster than temporary files?

Data moves through a kernel memory buffer, so there is no disk I/O or cleanup, and the next command can start processing before the first one finishes.

Why does a pipeline report success when an early command failed?

Without pipefail the exit status comes only from the last command. Enable `set -o pipefail` or inspect `PIPESTATUS`.

What is the difference between a pipe and a redirect?

A pipe connects one command's output to another command's input. A redirect connects a stream to a file or device.

Found a mistake, an outdated step or a broken link?