What is a pipe in Linux and how does it connect commands?
Learn what a Linux pipe (|) is and how it connects commands by streaming stdout into stdin, with real chaining examples and the Unix philosophy explained.
Expected Interview Answer
A pipe, written with the vertical bar (|), connects two commands so the standard output of the command on the left becomes the standard input of the command on the right, letting you chain small tools into one data-processing flow without temporary files.
The shell runs both commands concurrently and streams data between them through an in-memory buffer, so cat log.txt | grep ERROR | wc -l counts matching lines in a single stream. This embodies the Unix philosophy of small programs that each do one thing well and combine through text streams. Pipes carry only standard output, not standard error, and each stage reads its input as it is produced rather than waiting for the previous command to finish entirely.
- Combines simple tools into powerful workflows
- Avoids temporary intermediate files
- Streams data efficiently in memory
- Runs stages concurrently for speed
- Encourages small, reusable commands
AI Mentor Explanation
A pipe is like a relay of fielders returning the ball from the boundary. The deep fielder throws to the mid-fielder, who relays to the wicketkeeper — each hand passes the ball straight to the next without dropping it on the grass. Every fielder does one small job, and the ball flows through the chain just as data flows from one command's output into the next command's input.
Step-by-Step Explanation
Step 1
Understand standard streams
Every command has standard input, standard output and standard error; pipes connect stdout to the next command's stdin.
Step 2
Place the pipe symbol
Insert | between two commands so the left command's output feeds the right command's input.
Step 3
Chain multiple stages
Add further pipes, such as ps aux | grep node | wc -l, to filter then count in one flow.
Step 4
Know what flows
Only standard output travels through a pipe; standard error still goes to the terminal unless redirected.
Step 5
Combine with filters
Pair pipes with tools like grep, sort, uniq and awk to build a data pipeline without temporary files.
What Interviewer Expects
- Knows | connects stdout to the next command's stdin
- Understands the Unix small-tools philosophy
- Can give a real multi-stage example
- Distinguishes a pipe from output redirection (>)
- Aware that stderr is not piped by default
Common Mistakes
- Confusing a pipe (|) with redirection (> or >>)
- Thinking pipes pass standard error as well
- Assuming the left command must fully finish first
- Piping into a command that ignores stdin
- Overusing cat at the start of a pipeline unnecessarily
Best Answer (HR Friendly)
“A pipe lets you connect commands so the result of one is fed straight into the next, like an assembly line for data. It saves creating temporary files and lets you build a powerful task by joining several simple commands together.”
Code Example
# Count how many lines contain ERROR
cat app.log | grep ERROR | wc -l
# List running node processes, then count them
ps aux | grep node | grep -v grep | wc -l
# Show the five most common IPs in an access log
cut -d' ' -f1 access.log | sort | uniq -c | sort -nr | head -5Follow-up Questions
- How does a pipe differ from output redirection with >?
- How can you also pipe standard error?
- What is a named pipe (FIFO) and how does it differ?
- What does the tee command do in a pipeline?
- How do exit codes work in a multi-stage pipeline?
MCQ Practice
1. What does the pipe symbol | do?
A pipe sends the standard output of the left command into the standard input of the right command.
2. By default, which stream does a pipe NOT carry?
A plain pipe forwards only standard output; standard error still goes to the terminal unless explicitly redirected.
3. What does 'ls | wc -l' count?
ls outputs one entry per line and wc -l counts those lines, giving the number of listed entries.
Flash Cards
What symbol is a pipe? — The vertical bar |.
What does a pipe connect? — The stdout of the left command to the stdin of the right command.
Pipe vs redirection? — A pipe feeds another command; > sends output to a file.
Does a pipe carry stderr? — No, only stdout travels through a plain pipe by default.
Why use pipes? — To chain small tools into a workflow without temporary files.