Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: "${*:offset:length}" and ksh93/bash compatibility
On 3/13/22, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> Arriving to this a bit late, have been traveling.
>
> On Sun, Mar 13, 2022 at 8:22 AM Mikael Magnusson <mikachu@xxxxxxxxx> wrote:
>>
>> zsh% a=(one two three); echo "${a[*]:0:2}"
>> on
>> zsh% bash -c 'a=(one two three); echo "${a[*]:0:2}"'
>> one two
>
> The [*] subscript in double quotes has always been treated differently
> in zsh. You need [@] or the (@) modifier to preserve array-ness. I'm
> not sure the presence of the :off:len suffix should change that as a
> side-effect, at least not in native zsh mode ... and it might be
> difficult to "notice" the suffix early enough in the expansion code to
> cause that effect anyway. Particularly without also having the effect
> that the end result is not joined into a single string.
The way it works in bash is it does become a scalar, but the subscript
works on the array elements before joining (just like [1,3] does in
zsh in the same context). I'm reasonably sure that there would be no
difference in the zsh code for handling $*:X or $a[*]:X, other than
having to add the extra faked element for X=0. I think the correct
answer is to always have :off:len work on the array indices if they
exist, regardless of double-quoted-ness... But I also just took a look
at getindex() and don't want to replicate the appropriate parts of
that logic (which would require understanding what any of the parts
do).
I'm also not sure if it matters too much since it's been some years
before anyone even noticed it, I just (somewhat naively) thought that
maybe it wouldn't be too difficult to add...
--
Mikael Magnusson
Messages sorted by:
Reverse Date,
Date,
Thread,
Author