bug in /bin/sh?!?

Giorgos Keramidas keramida at ceid.upatras.gr
Mon Apr 7 02:22:26 UTC 2008


On Sun, 06 Apr 2008 11:17:18 -0700, Julian Elischer <julian at elischer.org> wrote:
> dino wrote:
>> Hello,
>>
>> on my FreeBSD 7.0-STABLE the line:
>>
>>> sh -c 'set -- ${HOME+A B C}; echo "1:$1"; echo "2:$2:"; echo "3:$3:"'
>>
>> prints
>>
>> 1:A B C:
>> 2::
>> 3::
>>
>> I would rather expect:
>>
>> 1:A:
>> 2:B:
>> 3:C:
>>
>> Is it correct that field splitting isn't performed on default/alternate
>> expanded values?
>
> "A B C" is a single value tha thappens to contain spaces.
> so, yes there is no splitting at that point.

I think there *is* splitting.

To inhibit splitting, you would have to use quotes, i.e. one of:

    set -- "${HOME+A B C}"; echo "1:$1"; echo "2:$2"; echo "3:$3"
    set -- ${HOME+"A B C"}; echo "1:$1"; echo "2:$2"; echo "3:$3"

Splitting does occur in bash, mksh, pdksh and zsh in FreeBSD.

The Solaris 10 version of /bin/sh shell does not accept the original at
all (I used Solaris 10 Update 3, aka "Solaris 10 11/06 s10x_u3wos_10
X86", for the test):

    $ sh -c 'set -- ${HOME+A B C}; echo "1:$1"; echo "2:$2:"; echo "3:$3:"'
    sh: bad substitution
    $

but it does accept any of the quoted options.  The /usr/bin/ksh shell on
Solaris accepts the original, but it splits the result at whitespace.



More information about the freebsd-hackers mailing list