Pipe multiple commands into a single command Pipe multiple commands into a single command shell shell

Pipe multiple commands into a single command


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

Example 1 (note that $ is the shell prompt):

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


Example 2:

(setopt; unsetopt; set) | sort


You can use {} for this and eliminate the need for a sub-shell as in (list), like so:

{ echo zzz; echo aaa; echo kkk; } | sort

We do need a whitespace character after { and before }. We also need the last ; when the sequence is written on a single line.

We could also write it on multiple lines without the need for any ;:

Example 1:

{  echo zzz  echo aaa  echo kkk} | sort

Example 2:

{  setopt  unsetopt  set} | sort


In Windows it would be as follow: (echo zzz & echo aaa & echo kkk) | sort

Or if it is inside a batch file it can be mono line (like sample) as well as multiline:

( echo zzz echo aaa echo kkk) | sort

Note: The original post does not mention it is only for Linux, so I added the solution for Windows command line... it is very useful when working with VHD/VHDX with diskpart inside scripts (echo diskpart_command) instead of the echo on the same, but let there the echo, there is also another way without echos and with > redirector, but it is very prone to errors and much more complex to write (why use a complicated prone to errors way if exists a simple way that allways work well)... also remember %d% gives you the actual path (very useful for not hardcoding the path of VHD/VHDX files).