Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Fix testsuite errors due to shell quoted parameter expansion issue.

[adding autoconf to document some shell bugs]

On 08/03/2010 02:32 PM, Ralf Wildenhues wrote:
> Interesting shell unportability:
> $ bash -c 'f=" val" e=; echo "$e"$f'
> val
> $ ksh -c 'f=" val" e=; echo "$e"$f'
>  val
> ksh93, dash, zsh all do it like ksh.  Is that a bug in bash?

Yes; adding bug-bash accordingly.  According to POSIX:


"After parameter expansion ( Parameter Expansion ), command substitution
( Command Substitution ), and arithmetic expansion ( Arithmetic
Expansion  ), the shell shall scan the results of expansions and
substitutions that did not occur in double-quotes for field splitting
and multiple fields can result."

Since $f is not quoted, its expansion must undergo field splitting.  But
since "$e" is quoted, it must not be elided even though empty.  The
result must be _two_ fields, as if you had done "echo '' 'val'".

But it is _also_ a bug in zsh; adding zsh-workers accordingly.

$ zsh -cvx 'f=" val" e=; echo "$e"$f'
+zsh:1> f=' val' e=''
+zsh:1> echo ' val'

Oops - zsh only passed one argument to echo, with a leading space,
instead of passing an empty argument and letting echo supply the space.
 ksh93, pdksh, and dash get it right (although dash doesn't use quotes
in -vx output, hence my use of n() to force things to tell; n() is
another way to expose the bash and zsh bugs).

$ ksh -cvx 'n() { echo $#; }; f=" val" e=; n "$e"$f'
n() { echo $#; }; f=" val" e=; n ""$f+ f=' val'
+ e=''
+ n '' val
+ echo 2

And meanwhile, I found a ksh93 parsing bug (don't know where to report

$ ksh -c 'a(){ echo hi; }; a'
ksh: syntax error at line 1: `}' unexpected
$ ksh -c 'a() { echo hi; }; a'
$ bash -c 'a(){ echo hi; }; a'
$ /bin/sh -c 'a(){ echo hi; }; a'

ksh is the only shell that requires a space between the ) and {, even
though the ) is a metacharacter and should not need trailing space (even
Solaris /bin/sh got that right).

Eric Blake   eblake@xxxxxxxxxx    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature

Messages sorted by: Reverse Date, Date, Thread, Author