I would like to re-direct stdout half-way through a script. How can I do this in Zsh?
# Do something # ... # Redirect stdout to a file (e.g. foo.txt) for x in *; print $x; done # Re-direct stdout back to default # Do something else # ...
When the scope of the redirection is a portion of code, it is simpler to put the redirection around that piece of code.
If you want to apply a redirection to a sequence of instructions, put it inside braces.
These constructs exist in all Bourne/POSIX-style shells, not just in zsh.
You can redirect the output of
This will only redirect the output of the
You can accomplish the same thing for pretty much any command or set of commands by using a subshell: