On Wednesday, April 17, 2013 02:40:44 PM David Korn wrote:
> cc: [email protected]
> Subject: Re: Re: [ast-developers] Reverse redirection / assignment order
> --------
>
> > 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.
> >
> >
>
> I don't get your point.
If assignments are evaluated before redirects, then reversing that order
requires only adding a command group, but if redirects are evaluated before
assignments, reversing the order is harder.
> > { var=$(foo) cmd; } 2>/dev/null
> will hide the error in ksh since redirections are done first for compound
> commands.
If cmd is a regular builtin, then in ksh the braces are redundant. In Bash or
for special builtins or a null cmd in ksh, this would flip the order forcing
the redirect first. This is fine, because it's easy enough to just always use
the redundant braces.
> var=$(foo) cmd 2>/dev/null
> will not hide assignment errors, but since the assignment is on the {...}
> it is done first.
It may or may not. Assuming this is ksh for a regular builtin, that _will_
hide assignment errors. If you wanted to reverse this it would require a
wrapper function or eval in order to evaluate the assignment first. That's why
I think the former is a better default for all simple commands (contrary to
POSIX).
The ordering can subtly affect things like exit status too. (I put together a
table of exceptional cases):
http://wiki.bash-hackers.org/dict/terms/exit_status?#portability
--
Dan Douglas
_______________________________________________
ast-developers mailing list
[email protected]
http://lists.research.att.com/mailman/listinfo/ast-developers