[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Differing behaviour of shells regarding simple commands with parameter assignments



Sorry for replying so late, but I disagree...

On 2012-12-26 03:03:57 +0100, Timo Weingärtner wrote:
> 2012-12-26, 02:22:45 Cyril Brulebois wrote:
> > Timo Weingärtner <timo@tiwe.de> (26/12/2012):
> > > bash, zsh, posh output 121
> > > 
> > > busybox sh, dash, (m)ksh output 122
> > > 
> > > checkbashisms doesn't complain.
> > > 
> > > Which of the three is wrong? Where shall I file bugs?
> > > 
> > > When bar is not a function but an external script the output is 121
> > > with all the shells.
> > 
> > you may want to check something like that:
> >  
> > http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#t
> > ag_02_09_05

This is not the latest version, but anyway... this isn't even the
right section for this problem.

> Thanks. If I read that correctly bash, zsh, posh are wrong, but I see that 
> changing that might cause trouble.

I disagree. This may be more clear with the following example:

bar() { /bin/true; }
baz() { foo=4; /bin/true; }

foo=1
echo -n $foo
foo=2 bar
echo -n $foo
foo=3 baz
echo -n $foo
baz
echo $foo
printenv foo || true

One gets 1114 with bash, posh and zsh, and 1244 with busybox sh, dash,
ksh93 and mksh. Moreover mksh also outputs "4" (i.e. it exports foo).

The behavior is described by:

  http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_01

[2.9.1 Simple Commands]. This text says in particular:

  If no command name results, variable assignments shall affect the
  current execution environment. Otherwise, the variable assignments
  shall be exported for the execution environment of the command and
  shall not affect the current execution environment (except for
  special built-ins).

We are in the "Otherwise" case, but there seems to be a contradiction
between "shall be exported for the execution environment of the command"
(which is, in the case of a function execution, the current environment)
and "shall not affect the current execution environment". But I think
that it should be interpreted that way: The second part means that the
value of foo is expected to be restored after the execution of the
function. So, the correct output is 1114. This behavior is more
intuitive IMHO: the behavior is the same (the environment is changed
locally), whether the command is a function or some other command found
in $PATH.

That said, I find the text very ambiguous...

Note also that zsh is not expected to be POSIX compliant (even with
"emulate sh", though it should try to behave as POSIX says). But I
think (as explained above) that here it is correct, like bash and
posh.

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <http://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


Reply to: