Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've inherited a script that does this in a function:

# IO redirection for logging.
#touch $LOGFILE
exec 6>&1           # Link file descriptor #6 with stdout.
                # Saves stdout.
exec > $LOGFILE     # stdout replaced with file $LOGFILE.

touch $LOGERR
exec 7>&2           # Link file descriptor #7 with stderr.
                # Saves stderr.
exec 2> $LOGERR     # stderr replaced with file $LOGERR.

Later on in the function it does this:

#Clean up IO redirection
exec 1>&6 6>&-      # Restore stdout and close file descriptor #6.
exec 1>&7 7>&-      # Restore stdout and close file descriptor #7.

The problem is if I call this function twice (i.e. in a loop) the second iteration stops at the first set of code above without showing an error.

Why would it be doing that?

share|improve this question

migrated from askubuntu.com Feb 7 '13 at 22:49

This question came from our site for Ubuntu users and developers.

I’m still not 100% sure what you mean. (A car stops when the driver steps on the brake, and it stops when it runs out of fuel –– do you see the difference?) More importantly, does my correction fix the problem? If not, what have you done to debug it? (Other suggestions: put $LOGFILE and $LOGERR into quotes; i.e., "$LOGFILE" and "$LOGERR". And if you intend to capture all the output from the function in the $LOG files (as opposed to just the last call), the second (third) command should be “exec >> "$LOGFILE"” and the fifth (sixth) one should be “exec 2>> "$LOGERR"”.) – Scott Feb 14 '13 at 18:05

I’m not exactly sure why it would “stop”, whatever you mean by that (does the script exit? hang?), I do see a problem: the last line should be exec 2>&7 7>&-.  (Also, the comment on that line should say “stderr” rather than “stdout”.)

share|improve this answer
By "stop" I mean the script stops executing after these lines with no further output and no error message – Dave Feb 13 '13 at 21:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.