Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Redirecting shell output to a pipe


If a shell script wants all its output to go to a file, it's easy:

	exec >filename 2>&1

Isn't it natural to assume that in the same fashion, you should also
be able to redirect the scripts output to a pipe? E.g., a very useful
idiom could have been

	exec | tee filename

to redirect stdout both to a file, and to the terminal.
But unfortunately, the above line does *not* work. Is there a reason why
it doesn't? Wouldn't this be a useful feature?

By the way, for the curious, there is actually a different solution to this
need, but it is much more convoluted. The other solution is to write this:

	exec 3>&1
	coproc tee /tmp/b >&3
	exec >&p 2>&1

(and perhaps do something, I'm not even sure what, at the end of the script
to give the tee coprocess enough time to finish outputting before the script

Another downside of this solution, besides being extremely complex (99%
of the zsh users will probably not be able to figure it out), is that
it takes the only coprocess that zsh gives you, which won't work if your
code is already using coprocesses.

Nadav Har'El                        |      Tuesday, Nov 24 2009, 7 Kislev 5770
nyh@xxxxxxxxxxxxxxxxxxx             |-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |"Guests, like fish, begin to smell after
http://nadav.harel.org.il           |three days." -- Benjamin Franklin

Messages sorted by: Reverse Date, Date, Thread, Author