ZSH Gem #6: Redirect output to multiple files

Posted by | Comments (4) | Trackbacks (0)

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):

Trackbacks

No Trackbacks for this entry.

Comments

There have been 4 comments submitted yet. Add one as well!
Jim
Jim wrote on : (permalink)

The final point is just as true under bash, but the multiple redirections is cool. I’m at a loss as to what you’re trying to do with this though:

cat < file1 < file2 > cat file3

Is the second ‘cat’ just an editing mistake? This would suggest that file3 is a parameter to the original cat command, and the second ‘cat’ is an output file name. Have I missed something?

Thanks for an interesting series… switching shells is no minor undertaking, but I’m liking what I’m seeing here.

Janek Bevendorff
Janek Bevendorff wrote on : (permalink)

No, it isn’t a mistake. This example streams the contents of file1 and file2 to STDIN of the cat command which then outputs it as STDOUT to file3. So in the end you get the same as cat file1 file2 > file3. I know, in this particular example it doesn’t make a lot of sense, but there may be other use cases where this can indeed become helpful.

EDIT: Whoops, just saw what you meant. There was a second cat in the command chain which I constantly overlooked. Thanks for that. I’ve corrected it.

Simon
Simon wrote on : (permalink)

Something you didn’t mention directly is that you can redirect a stream to a file and see it in the console (like with tee) with:

% echo "foo" >&1 > file foo % cat file foo

Janek Bevendorff
Janek Bevendorff wrote on : (permalink)

Yeah, that’s right. The only thing where you still need tee is when you need root rights to access the file:

pre. echo ‘foobar’ | sudo tee file

Write a comment:

HTML-Tags will be converted to Entities.
Markdown format allowed
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.

By submitting a comment, you agree to our privacy policy.

Design and Code Copyright © 2010-2020 Janek Bevendorff Content on this site is published under the terms of the GNU Free Documentation License (GFDL). You may redistribute content only in compliance with these terms. tweetbackcheck