ZSH Gem #6: Redirect output to multiple files
One unique feature in ZSH is that you can redirect streams to multiple outputs or inputs simultaneously. With this multi-stream redirection you can, e.g., redirect STDOUT OR STDERR of a program to more than one file at the same time without using a command line tool such as tee
.
When the option MULTIOS
is set (which is the default), ZSH opens all the file descriptors as pipes when there is more than one. This allows you to open more than one file descriptor at the same time using the normal redirection operators:
echo 'foobar' > file1 > file2
But of course you can also use multiple input streams:
cat < file1 < file2
and you can also combine both:
cat < file1 < file2 > file3
(yes, I know, the examples are stupid and maybe candidates for the next “useless use of …” award, but I hope you got the point)
This is not a huge feature, but it's good to know and sometimes it comes in very handy. For instance, it makes it much easier to process multiple streams separately. For example, you can redirect STDOUT to one file and STDERR to another:
somecommand > stdoutfile 2> stderrfile
Simple but useful.
Read more about multi-stream redirection (multios):