I need references and opinions about the following, please.

Consider:

{
        exec 8</some/file
        ...stuff using file descriptor 8...
} 8<&-

Should the effect of the 'exec' persist past the compound command (the curly braces block)?

My expectation is that the '8<&-' should push file descriptor 8 onto the shell's internal stack in a closed state, so that it is restored at the end of the block.

I think this should allow the effect of 'exec' to be local to that compound command, and I think this construct should be nestable.

According to my testing, nearly all shells do this. However, two very widely used shells, dash and bash, leave the file descriptor open beyond the block.

The author of dash, Herbert Xu, said in response to my bug report that dash is not obligated to push and restore that file descriptor if it is already closed when entering the compound command -- implying that a file descriptor should not be pushed if the local state would be different from the parent state.

He asked me for a POSIX reference proving otherwise. I can't find any.

Without this behaviour, an awkward workaround is required. To guarantee that the FD will be restored at the end of the block, you'd need to attempt to push it twice in two different states in two nested compound command blocks, for instance:

{
        {
                exec 8</some/file
                ...stuff using file descriptor 8...
        } 8<&-
} 8>/dev/null

Does anyone have pointers to the POSIX text, or other strong evidence, that this workaround should not be necessary?

Thanks,

- M.

Reply via email to