Restoring stdout and stderr to default value Restoring stdout and stderr to default value shell shell

Restoring stdout and stderr to default value


This example

Example 20-2. Redirecting stdout using exec

#!/bin/bash# reassign-stdout.shLOGFILE=logfile.txtexec 6>&1           # Link file descriptor #6 with stdout.                    # Saves stdout.exec > $LOGFILE     # stdout replaced with file "logfile.txt".# ----------------------------------------------------------- ## All output from commands in this block sent to file $LOGFILE.echo -n "Logfile: "dateecho "-------------------------------------"echoecho "Output of \"ls -al\" command"echols -alecho; echoecho "Output of \"df\" command"echodf# ----------------------------------------------------------- #exec 1>&6 6>&-      # Restore stdout and close file descriptor #6.echoecho "== stdout now restored to default == "echols -alechoexit 0

appears to show what you want. It came from the ABS, where there is a small amount of discussion and other relevant information.


Try the "tee" command:

exec | tee outputfile

Look at the tee manpage for more explainations:

tee - read from standard input and write to standard output and files