If you really need to switch it back and forth without knowing in advance what will be, where and when, this is pretty much the way to do it. Depending on your requirements, although it may be more neat to isolate parts that need redirection and execute them as a group, for example:
echo first { echo something cat kjkk } 1>outfile 2>&1 if some_predicate outfile; then echo ERROR; fi
{} is called a group command, and the output of the entire group is redirected. If you prefer, you can do your execs in a subshell, since this only affects the subshell.
echo first ( exec 1>outfile 2>&1 echo something cat kjkk ) if some_predicate outfile; then echo ERROR; fi
Please note that here I am using parentheses () , not curly braces {} (which were used in the first example).
NTN
falstro
source share