Pipes
A pipe is like a bucket line where one kid passes what they have to the next kid. Each command does its small job and hands the result forward.
In this page:
Syntax
command1 | command2 | command3
Pipes
The pipe operator | connects the standard output of one command directly to the standard input of the next, letting you chain small commands into a data-processing pipeline.
Bash runs both commands concurrently, streaming data between them. Pipelines are one of the most powerful and idiomatic features of shell scripting.
Note:
Chain several pipes together, like
cmd1 | cmd2 | cmd3, to build a multi-step data pipeline in one line.Example: Pipes
#!/bin/bash
printf "banana\napple\ncherry\n" | sort
echo "---"
printf "a\nb\nc\nd\n" | wc -l
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Expecting a pipe to send error messages (stderr), when only standard output goes through it.
- Setting variables inside a
while readloop at the end of a pipe and expecting them after the loop, since it runs in a subshell. - Using
cat file | grep xwheregrep x fileis enough.
🔒
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: