← Back to Bash Course | Chapter 3: Control Flow | Lesson 7 of 14

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:

  1. Pipes
Syntax
bash
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

bash
#!/bin/bash
printf "banana\napple\ncherry\n" | sort

echo "---"

printf "a\nb\nc\nd\n" | wc -l
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
  1. Expecting a pipe to send error messages (stderr), when only standard output goes through it.
  2. Setting variables inside a while read loop at the end of a pipe and expecting them after the loop, since it runs in a subshell.
  3. Using cat file | grep x where grep x file is enough.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.