exec - (built-in) used to fork new processes.
exec - (built-in) when used with redirection,
allows additional file descriptors.
< - stdin, > - stdout, and 2> - stderr
are standard file descriptors and are set to your display terminal.
Run tty to see what it actually is.
Use exec to assign/create additional file descriptors.
# Open file descriptor to a file.
exec 3> listing
# Redirect command's standard out to the file descriptor 3.
# All commands' outputs will be appended to single file.
ls >&3
ls -l >&3
# Close file descriptor 3 by setting to null, can't use anymore.
exec 3>&-
cat listing
# Re-initialize file descriptor 3 and file1.
exec 3> listing
#This works, but output from previous command[s] gone.
ps >&3
exec 3>&-
cat listing
Alternatively, you can use a file descriptor to store terminal's ID
# initialize a new file descriptor to terminal, use stdout for source
exec 6>&1
# now redirect stdout to a target file.
exec > target-file
#command output should go to target-file
echo "mylist"
ls
#Reset file descriptor 1 to terminal (stored in 6).
exec 1>&6
#Close the temp file descriptor
exec 6>&-
#command output should go to terminal screen.
echo "I'm home"
This will also work for standard input (0).
check out :
http://wiki.bash-hackers.org/howto/redirection_tutorial
http://tldp.org/LDP/abs/html/x17974.html