.. _intro_zsh_redirection:

Redirection
===========

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

.. code-block:: text
   :caption: Redirect to multiple files
   :name: redirection_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:

.. code-block:: text
   :caption: Redirect and pipe together
   :name: redirection_and_pipe

   % make > make.log | grep Error

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

.. code-block:: text
   :caption: Input from multiple files
   :name: redirection_multiple_inputs

   % 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.

.. code-block:: text
   :caption: Pipe then more input
   :name: redirection_pipe_then_file

   % cut -d: -f1 /etc/passwd | sort <newnames

The sort will get as its standard input first the output of :literal:`cut` and then the contents of :literal:`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 :literal:`zsh` is by redirecting the standard error using :literal:`2> >(`...\ :literal:`)`.

.. code-block:: text
   :caption: Stderr via process sub
   :name: redirection_stderr_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.
