Q1 . There is an easy way to restore stdout to the terminal after being redirected to a file:
exec >/dev/tty
When storing the original file descriptor stdout usually requires that it be restored later, in this particular case, you want stdout be /dev/tty , so there is no need to do more.
$ date Mon Aug 25 10:06:46 CEST 2014 $ exec > /tmp/foo $ date $ exec > /dev/tty $ date Mon Aug 25 10:07:24 CEST 2014 $ ls -l /tmp/foo -rw-r--r-- 1 jlliagre jlliagre 30 Aug 25 10:07 /tmp/foo $ cat /tmp/foo Mon Aug 25 10:07:05 CEST 2014
Q2 : exec 1>file is a slightly more detailed way to make exec >file , which, as already mentioned, redirects stdout to this file if you have the right to create / write it. A file is created if it does not exist, it is truncated if it does.
exec 1>&- closes stdout, which is probably a bad idea in most situations.
Q3 : teams should be
exec 0<&- exec 1>&- exec 2>&-
Note the reverse redirection for stdin.
This can be simplified as follows:
exec <&- >&- 2>&-
This command closes all three standard file descriptors. This is a very bad idea. If you want the script to be disconnected from these channels, I would suggest this more robust approach:
exec </dev/null >/dev/null 2>&1
In this case, all output will be discarded instead of triggering an error, and all input will return only nothing instead of failure.
source share