8. Redirection

Apart from all the regular redirections like the Bourne shell has, zsh can do more. You can send the output of a command to more than one file, by specifying more redirections like

Example 8.1: Redirect to multiple files
% echo Hello World >file1 >file2

and the text will end up in both files. Similarly, you can send the output to a file and into a pipe:

Example 8.2: Redirect and pipe together
% make > make.log | grep Error

The same goes for input. You can make the input of a command come from more than one file.

Example 8.3: Input from multiple files
% sort <file1 <file2 <file3

The command will first get the contents of file1 as its standard input, then those of file2 and finally the contents of file3. This, too, works with pipes.

Example 8.4: Pipe then more input
% cut -d: -f1 /etc/passwd | sort <newnames

The sort will get as its standard input first the output of cut and then the contents of newnames.

Suppose you would like to watch the standard output of a command on your terminal, but want to pipe the standard error to another command. An easy way to do this in zsh is by redirecting the standard error using 2> >().

Example 8.5: Stderr via process sub
% find / -name games 2> >(grep -v 'Permission' > realerrors)

The above redirection will actually be implemented with a regular pipe, not a temporary named pipe.