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

Expansion/quoting quirks


albeit Zoltan's claims of improved sh-compatibility are almost true,
there is still a serious bug in his release that prevents the ${1+"$@"}
idiom from doing the right thing, because quoted arguments with embedded
whitespace are split despite their quotes. To demonstrate this bug and a
few other possible problems I threw together a tiny test script, which is
appended below.

Incidentally, the only shell I have access to, which passes all the tests
of my script, is `PD KSH v5.1.3 94/12/22'. Even bash has trouble with null
args. Other contestants were 2.6-beta11-test12 and 2.6-beta11-test9-hzoli11,
each of them invoked as `./sh -f' and `./ksh -f', respectively.

Save as `expbug', set symlinks from sh (and ksh) to zsh, and execute with
`expbug ./sh -f', for example. The testee is running as a coprocess, and
its output will be compared to the expected output, which is on the lines
beginning with `#%'. Sample output (for hzoli) looks like this:

  $ ~/expbug ./ksh -f
  Testing ./ksh -f ...
  < \
  < argc=1, argv=( 'a b' )
  > argc=2, argv=( 'a' 'b' )
  > argc=4, argv=( 'a' 'b' '' 'c' )
  < argc=3, argv=( 'a b' '' 'c' )
  < argc=3, argv=( 'a b' '' 'c' )
  > zsh: closing brace expected

  Not OK, output left in file /tmp/zshtest18454

If I'm missing something, I'm sure, you'll tell me. :-)

==================== cut here ==================== 
#!/bin/zsh -f
# expbug: a few (k)sh-compatibility tests
    if (( $# ))
        WHAT=(zsh -f)
    echo "Testing $WHAT ..."
    coproc 2>&1 $WHAT
    sed -ne "/^#/!p" -e "s/^#%[ ]*//w $TEMP" >&p
    if <&p cat -v | diff $TEMP -
        echo OK.
        rm -f $TEMP
        exit 0
        echo "\nNot OK, output left in file $TEMP"
        exit 1
) <<\EOF

# Command substitution eats too much backslashes
#% \
echo `echo \\\\`    # broken in hzoli, and in vanilla zsh if invoked as (k)sh
#% \
echo $(echo \\\\)
#% \
echo "$(echo \\\\)" # sh and ksh seem to differ here (bash would give `\\')

# Single quotes aren't recognized (fixed in hzoli)
#% $foo
foo=10 x=foo
echo $y

# Backslash ignored between single quotes (fixed in hzoli)
#% \$x
echo $(echo '\$x')

# nargs(): A function which neatly prints the arguments passed to it
nargs () {
  echo -n "argc=$#, argv=( "
  while [ $# -ne 0 ]; do
    echo -n "'$1' "
  echo ")"

#% argc=2, argv=( 'a' 'b' )
nargs ${undef-a b}
#% argc=1, argv=( 'a b' )
nargs ${undef-"a b"}             # vanilla + hzoli: shouldn't split here
#% argc=1, argv=( 'a b' )
nargs "${undef-a b}"

# The following are broken in vanilla

set "a b" "" c

#% argc=3, argv=( 'a' 'b' 'c' )
nargs ${undef-$@}                # This works in hzoli's sh-mode 

#% argc=3, argv=( 'a b' '' 'c' )
nargs ${undef-"$@"}              # hzoli: 'a b' shouldn't split into 'a' 'b'

#% argc=3, argv=( 'a b' '' 'c' )
nargs "${undef-$@}"              # This works also in hzoli's sh-mode

#% argc=3, argv=( 'a b' '' 'c' )
nargs "${undef-"$@"}"            # hzoli: zsh: closing brace expected

==================== cut here ==================== 

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