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# ... 
Redirect output half-way through a script, and restore later
zsh;io redirection
Use the exec builtin with a redirection but no command to redirect the shell's file descriptors. If you want to restore the original destination, you'll have to save it in another descriptor.exec 3>&1 >foo.txtfor x in *; do  print $xdoneexec >&3 3>&-When the scope of the redirection is a portion of code, it is simpler to put the redirection around that piece of code.for x in *; do  print $xdone >foo.txtIf you want to apply a redirection to a sequence of instructions, put it inside braces.{  for x in *; do    print $x  done;  echo 'one more line'} >foo.txtThese constructs exist in all Bourne/POSIX-style shells, not just in zsh.