[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Unquoted array slice ${a[@]:0} expands to just one word if IFS doesn't h
From: |
Ilkka Virta |
Subject: |
Unquoted array slice ${a[@]:0} expands to just one word if IFS doesn't have a space |
Date: |
Wed, 1 Aug 2018 14:43:27 +0300 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 |
On both Bash 4.4.12(1)-release and 5.0.0(1)-alpha, a subarray slice like
${a[@]:0} expands to just one word if unquoted (and if IFS doesn't
contain a space):
$ a=(aa bb); IFS=x; printf ":%s:\n" ${a[@]:0}
:aa bb:
I expected it would expand to separate words, as it does without the
slice, and just like $@ does, sliced or not:
$ a=(aa bb); IFS=x; printf ":%s:\n" ${a[@]}
:aa:
:bb:
$ set -- aa bb; IFS=x; printf ":%s:\n" $@
:aa:
:bb:
$ set -- aa bb; IFS=x; printf ":%s:\n" ${@:1}
:aa:
:bb:
It's as if it first joins the picked elements with spaces, and then
splits using IFS, instead of producing multiple words and word-splitting
them individually.
The same thing happens with ${a[*]:0} (but not with ${*:1}):
the array elements get joined with spaces to a single word. If IFS is
empty, unset, or contains a space the result is multiple words as
expected with both [@] and [*].
An expansion like that should in most cases be quoted,
but the current behaviour still seems a bit inconsistent.
--
Ilkka Virta / itvirta@iki.fi
- Unquoted array slice ${a[@]:0} expands to just one word if IFS doesn't have a space,
Ilkka Virta <=
Re: Unquoted array slice ${a[@]:0} expands to just one word if IFS doesn't have a space, Chet Ramey, 2018/08/02