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.
Practical Pipe Patterns
# 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 messagesgrep 'ERROR' /var/log/app.log | sort | uniq -c | sort -rn | head -5 # Parse JSON from an APIcurl -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 filemkfifo /tmp/mypipeecho "data" > /tmp/mypipe &cat /tmp/mypiperm /tmp/mypipeHow 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.
false | true ; echo $? # 0, the failure is masked set -o pipefailfalse | true ; echo $? # 1, now it surfaces grep pattern file | wc -lecho "${PIPESTATUS[@]}" # exit code of each stageTroubleshooting 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 |
TipUse
teeto keep a full copy while filtering:command | tee full.log | grep ERROR.
Common MistakeExpecting a pipe to carry error output. Only stdout flows through
|. Add2>&1(or use|&in bash) to include stderr.
RememberUse
set -o pipefailin 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.