Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Bash Dictionary
  3. | (Pipe)

| (Pipe)

A pipe (|) passes the standard output of one command directly to the standard input of the next. By chaining multiple commands together, you can express complex processing as a one-liner. It is one of the most important features of Bash.

Syntax

command1 | command2 | command3 ...

Use xargs to pass pipe output as arguments.

command1 | xargs command2

Pipe-related Syntax

SyntaxDescription
cmd1 | cmd2Passes the standard output of cmd1 to the standard input of cmd2.
cmd1 |& cmd2Passes both standard output and standard error to cmd2 (Bash 4+).
cmd1 2>&1 | cmd2Merges standard error into standard output and passes both to cmd2.
cmd1 | tee file | cmd2Saves the output of cmd1 to a file while also passing it to cmd2.
cmd1 | xargs cmd2Passes the output of cmd1 as arguments to cmd2 (one argument per line).
cmd1 | xargs -I{} cmd2 {}Runs cmd2 for each line, substituting the line into the {} placeholder.
${PIPESTATUS[@]}Returns the exit status of each command in the pipeline as an array.

Sample Code

The following examples use this file.

fruits.txt
cherry
apple
banana
apple
cherry

Sort the lines in a file and remove duplicates. sort orders the lines, then uniq removes consecutive duplicate lines.

cat fruits.txt | sort | uniq
apple
banana
cherry

Use grep to extract only error lines from a log file, then display the last 10.

grep "ERROR" app.log | tail -10
2026-03-06 09:15:22 ERROR: Connection refused
2026-03-06 09:18:03 ERROR: Timeout exceeded

Check running nginx processes. The key is using grep -v grep to exclude the grep process itself from the results.

ps aux | grep nginx | grep -v grep
www-data  1234  0.0  0.2  55840  4096 ?  Ss  10:00  0:00 nginx: master
www-data  1235  0.0  0.1  55840  2048 ?  S   10:00  0:00 nginx: worker

Count the number of files in the current directory.

ls | wc -l
42

Display the top 10 entries by disk usage, largest first.

du -sh * | sort -h | tail -10

Pipe standard error along with standard output, and use tee to also save everything to a log file.

make 2>&1 | tee build.log | grep -E "error|warning"

Use find to locate files and xargs to convert the results into arguments for deletion.

find . -name "*.tmp" | xargs rm

Use ${PIPESTATUS[@]} to check the exit status of each command in the pipeline.

cat fruits.txt | grep "apple" | wc -l
2
echo ${PIPESTATUS[@]}
0 0 0

Notes

A pipe (|) runs each command in a separate process in parallel, making it very efficient. However, if you assign a value to a shell variable inside a pipe, that variable is not accessible outside the pipe because each segment runs in its own subshell. To capture output into a variable, use command substitution ($()) instead.

To save output to a file while also passing it to the next command, see tee.

If you find any errors or copyright issues, please .