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

Re: Redirecting shell output to a pipe



Nadav Har'El wrote:
> Hi,
>
> 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
>   

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
> exits).
>
> 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.
>
>   



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