Saving and restoring standard file descriptors of shell scripts - Help

This is a discussion on Saving and restoring standard file descriptors of shell scripts - Help ; How can a shell script save and restore its standard file descriptors (stdin, stdout and stderr)?...

+ Reply to Thread
Results 1 to 2 of 2

Thread: Saving and restoring standard file descriptors of shell scripts

  1. Saving and restoring standard file descriptors of shell scripts

    How can a shell script save and restore its standard file descriptors (stdin, stdout and stderr)?

  2. Re: Saving and restoring standard file descriptors of shell scripts

    Angel Tsankov wrote:

    > 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 ...)

+ Reply to Thread