Date:        Mon, 3 Jul 2017 09:07:33 +0200
    From:        Martijn Dekker <mart...@inlv.org>
    Message-ID:  <f5c917b6-cb43-f6e2-2292-19b4c0b32...@inlv.org>

  | Should the necessity of these curly braces be considered a bug?

This relates to posix XCU 2.9.1 and the steps to use to execute a
simple command (which is what is here when the { } are not there.)

This is a 4 step (ordered) process, and the order is

        1 Move var assigns and redirects out of the word list
                (in this case that is everything, and there is nothing left)
        2 expand the remaining words (none in this case)
        3 Perform redirections
        4 expand & perform variable assignments

If that was all, you'd be correct, and the 3>&1 should have been performed
before the cmd sub in the var expansion needs it.

However, 2.9.1 goes on to say ...

        In the preceding list, the order of steps 3 and 4 may be reversed
        if no command name results from step 2 or if the command name matches
        the name of a special built-in utility; see Section 2.14.

Since no command name resulted from step 2 in this case, the shell is
allowed to do the var assignments first, and the redirections after, which
is obviously what is happening with the shells which "don't work".
(There is no special built-in here, and so 2.14 is not relevant.)

When the { } are there there's no question the redirect on the compound
command itself happens first, and then the contents of that are executed,
so then it all works.

Personally I think the "may be reversed" (not even "shall be") is a mistake,
and just pandering to bugs in old shells, but this is the world we live in
and applications simply cannot rely, either way, on the evaluation order
when there is no command name (or on special built-ins.)

kre

Reply via email to