A pipe connects stdout of one command to stdin of the next— the Unix “text as interface” pattern that makes small tools powerful.
Basic pipes
ls | wc -l
cat /etc/passwd | head -3
cat /etc/passwd | grep bash | head -2Each stage should do one job well—compose instead of writing monolithic scripts.
Pipeline exit status
false | true
echo "pipe status: ${PIPESTATUS[0]} ${PIPESTATUS[1]}"PIPESTATUS holds exit codes for each command in the last pipeline (Bash).
tee
echo "data" | tee copy.txt | wc -ctee writes a copy to a file while passing data downstream—handy for logging and processing.
Important interview questions and answers
- Q: What does | do?
A: Connects stdout of the left command to stdin of the right. - Q: PIPESTATUS vs $?
A: $? reflects the last command in the pipeline; PIPESTATUS lists each stage.
Self-check
- Which command counts lines in ls output?
- What variable stores per-stage exit codes in Bash?
Tip: With set -o pipefail, any failing stage fails the pipeline—essential in scripts.
Interview prep
- Pipe connects?
Left stdout to right stdin.
- pipefail?
Pipeline fails if any command fails.