Rob Bednark Rob Bednark - 1 year ago 71
Bash Question

Pipe multiple commands to a single command

How can I pipe the stdout of multiple commands to a single command?

Example 1: combine and sort the output of all three echo commands:

echo zzz; echo aaa; echo kkk

desired output:


Example 2: rewrite the following so that all the commands are in a single command-line using pipes, without redirects to a temp file:

setopt > /tmp/foo; unsetopt >> /tmp/foo; set >> /tmp/foo; sort /tmp/foo

Answer Source

Use parentheses ()'s to combine the commands into a single process, which will concatenate the stdout of each of them.

Example 1:

$ (echo zzz; echo aaa; echo kkk) | sort

Example 2:

(setopt; unsetopt; set) | sort
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download