FDRESERVE(1)

NAME
fdreserve - update the environment with file descriptors that are guaranteed safe to use, then execute a program

SYNOPSIS
fdreserve n prog...

DESCRIPTION
fdreserve tries to reserve n file descriptors.

fdreserve sets the FD0, FD1, ..., FDn-1 environment variables: each FD i contains a valid file descriptor, that can be safely opened.

fdreserve then exec(3) s into prog with its arguments.

  Common use
fdreserve can be used when you do not want to hardcode file descriptors in your scripts. For instance, to create a pipe, you could use:

    #!/command/execlineb
    fdreserve 2
    multisubstitute
    {
      importas fdr FD0
      importas fdw FD1
    }
    piperw $fdr $fdw
    prog...

Warning: fdreserve does not allocate descriptors, it merely returns descriptors that are free at the time it is run. A program like

    #!/command/execlineb
    fdreserve 3
    multisubstitute
    {
      importas fdr FD0
      importas fdw FD1
    }
    piperw $fdr $fdw
    fdreserve 1
    multisubstitute
    {
      importas oldfd FD2
      importas newfd FD0
    }
    prog...

may fail, because oldfd and newfd may be the same. To avoid that, you should make sure that all descriptors returned by fdreserve are actually allocated before calling fdreserve again. (Thanks to Paul Jarc[1] for having spotted that case.)

SEE ALSO
emptyenv(1), envfile(1), exec(1), execline-cd(1), execline-umask(1), exit(1), export(1), fdblock(1), fdclose(1), fdmove(1), fdswap(1), getcwd(1), getpid(1), heredoc(1), piperw(1), posix-cd(1), posix-umask(1), redirfd(1), trap(1), tryexec(1), unexport(1), wait(1), withstdinas(1), exec(3)

[1] https://code.dogmap.org/

This man page is ported from the authoritative documentation at: https://skarnet.org/software/execline/fdreserve.html

AUTHORS
Laurent Bercot Alexis <flexibeast@gmail.com> (man page port)