Discussion:
Saving and restoring standard file descriptors of shell scripts
(too old to reply)
Angel Tsankov
2006-08-12 17:44:53 UTC
Permalink
How can a shell script save and restore its standard file descriptors (stdin, stdout and stderr)?
kermit
2006-08-14 16:47:20 UTC
Permalink
Post by Angel Tsankov
How can a shell script save and restore its standard file descriptors
(stdin, stdout and stderr)?
one common trick is

# move FD 1 to FD 20
exec 20>&1 1>&/some/other/file
...

# move FD 1 back to original
exec 1>&20

this assumes that shell supports multidigit FD number (some older shells do
not) and FD 20 is not used (e.g. it may be inherited and contain SSH agent
connection ...)

Loading...