On Wednesday, April 17, 2013 02:57:38 PM Cedric Blancher wrote:
> David?
>
> Ced
>
> On 10 January 2013 23:06, Cedric Blancher
> <[email protected]> wrote:
> > David, do you have any idea why ksh93 doesn't use POSIX order in the
> > example below?
> >
> > Ced
> >
> > ---------- Forwarded message ----------
> > From: Dan Douglas <[email protected]>
> > Date: 9 January 2013 20:00
> > Subject: Reverse redirection / assignment order
> > To: [email protected]
> >
> > When expanding simple commands, steps 3 and 4 are reversed unconditionally
> > for all command types and number of words expanded, even in POSIX mode.
> > http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_01
> >
> > The exceptions allowed by POSIX appear to only apply to ksh93. Other shells
> > always use the POSIX order, except Bash, which never uses the POSIX order,
> > though the manpage description is the same as POSIX.
> >
> > #!/usr/bin/env bash
> >
> > # 1) no command expanded, 2) special builtin, 3) regular builtin.
> > tst() {
> > "$sh" -c 'x=$(printf 2 >&2) ${1+"$1"} <&0$(printf 1 >&2)' _ "$@"
> > } 2>&1
> >
> > for sh in {,{b,d}a,po,{,m}k,z}sh bb; do
> > printf '%-4s: %s %s %s\n' "$sh" "$(tst)" "$(tst :)" "$(tst true)"
> > done
> >
> > Out:
> > sh : 21 21 21 # bash posix mode
> > bash: 21 21 21 # normal mode
> > ksh : 21 21 12 # ksh93 is the other oddball shell
> > dash: 12 12 12 # ...
> > ... # Everything else same as dash
> >
> > I don't know why this order was chosen or what the advantages to one over
> > the other might be.
> > --
> > Dan Douglas
In retrospect, the Bash way makes sense. To force reverse evaluation for the
redirect first in Bash only requires wrapping with a command group. If the
redirect evaluates first by default, then reversing that to evaluate an
assignment first is much harder, since assignments can't precede compound
commands.
# Hide foo's err in bash
{ var=$(foo) cmd; } 2>/dev/null
versus (e.g. if cmd is a ksh-style function)
# Don't hide foo's err in ksh
var=$(foo) command eval cmd 2\>/dev/null
{ var=$(foo >&3) cmd 2>/dev/null; } 3>&1
The former is obviously better, at the cost of breaking anything that
relies on the standard behavior. Apparently nobody's complained in the last 2
decades.
--
Dan Douglas
_______________________________________________
ast-developers mailing list
[email protected]
http://lists.research.att.com/mailman/listinfo/ast-developers