Lawrence Velázquez wrote, on 12 Jan 2025:
>
> On Fri, Jan 10, 2025, at 7:50 PM, Christoph Anton Mitterer via austin-group-l 
> at The Open Group wrote:
> > but doing something more complex like:
> >    command exec 3>&1 || return 125
> >    captured_stderr="$( command exec 2>&1 >&3 3>&- || exit 125; utility 
> > )"  ||  exit_status="$?"
> >    command exec 3>&- || return 125
> >
> > is not guaranteed to work.
> 
> Not in general, but for this specific example I don't see why not.
> It's portable to use FD 3 inside the command substitution because
> subshells aren't utilities, and the actual "utility" clearly doesn't
> try to use FD 3, given that you close it first.
> 
> And behold, it works with the four FD-closing shells yet mentioned:

I wonder whether perhaps Christoph was thinking that this wording:

    If any file descriptors with numbers greater than 2 are opened by
    those redirections, it is unspecified whether those file
    descriptors remain open when the shell invokes another utility.

means that in this part of the command substitution:

    command exec 2>&1 >&3 3>&-

"command" is "another utility" and so fd 3 could be closed when the
shell executes "command", and thus it would not be open when "command"
executes "exec", and the >&3 would fail.  However, the sequence of
processing specified in POSIX (in XCU 2.9.1) is that the redirections
are performed before it executes "command", so fd 3 would still be open
when the >&3 is done.

So I think his specific use case is not a problem; however I think
his mail has brought to light a mismatch between the POSIX wording and
implementations.  As I understand it, in ksh this is implemented by
setting FD_CLOEXEC on file descriptors greater than 2 when opened by
"exec".  This means it would never affect built-in utilities, so I
think a better match would be:

    ..., it is unspecified whether those file descriptors remain open
    when the shell invokes a non-built-in utility.

I can't think of any built-ins in POSIX that have the ability to access
a specified fd, but it would matter if we ever want to add "read -u" to
the standard.

-- 
Geoff Clare <[email protected]>
The Open Group, Apex Plaza, Forbury Road, Reading, RG1 1AX, England

  • exec redirec... Christoph Anton Mitterer via austin-group-l at The Open Group
    • Re: exe... Lawrence Velázquez via austin-group-l at The Open Group
      • Re:... Oğuz via austin-group-l at The Open Group
      • Re:... Chet Ramey via austin-group-l at The Open Group
        • ... Martijn Dekker via austin-group-l at The Open Group
          • ... Christoph Anton Mitterer via austin-group-l at The Open Group
            • ... Lawrence Velázquez via austin-group-l at The Open Group
    • Re: exe... Lawrence Velázquez via austin-group-l at The Open Group
      • Re:... Geoff Clare via austin-group-l at The Open Group
        • ... Lawrence Velázquez via austin-group-l at The Open Group
          • ... Geoff Clare via austin-group-l at The Open Group
        • ... Christoph Anton Mitterer via austin-group-l at The Open Group
          • ... Geoff Clare via austin-group-l at The Open Group
      • Re:... Christoph Anton Mitterer via austin-group-l at The Open Group
    • Re: exe... Harald van Dijk via austin-group-l at The Open Group
      • Re:... Christoph Anton Mitterer via austin-group-l at The Open Group
        • ... Oğuz via austin-group-l at The Open Group
      • Re:... Christoph Anton Mitterer via austin-group-l at The Open Group
        • ... Geoff Clare via austin-group-l at The Open Group

Reply via email to