Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #14431

Re: Unquoted array slice ${a[@]:0} expands to just one word if IFS doesn't have a space

From Ilkka Virta <itvirta@iki.fi>
Newsgroups gnu.bash.bug
Subject Re: Unquoted array slice ${a[@]:0} expands to just one word if IFS doesn't have a space
Date 2018-08-01 16:06 +0300
Message-ID <mailman.4550.1533128777.1292.bug-bash@gnu.org> (permalink)
References <c1978119-1836-e88d-4889-c54bea4d0559@iki.fi> <20180801121251.efyquoibrsneiqbl@eeg.ccf.org>

Show all headers | View raw


On 1.8. 15:12, Greg Wooledge wrote:
> On Wed, Aug 01, 2018 at 02:43:27PM +0300, Ilkka Virta wrote:
>> 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):
> 
> This just reinforces the point that unquoted $@ or $* (or the array
> equivalent) is a bug in the script.  It gives unpredictable results.

Unquoted $* seems well-defined in Bash's reference manual:

   ($*) Expands to the positional parameters, starting from one. When the
   expansion is not within double quotes, each positional parameter
   expands to a separate word.

The reference doesn't really say anything about an unquoted $@, but then 
there's the POSIX definition which should be well-defined in this case, 
since clearly field-splitting should be performed here.

   @: Expands to the positional parameters, starting from one, initially
   producing one field for each positional parameter that is set. When
   the expansion occurs in a context where field splitting will be
   performed, any empty fields may be discarded and each of the non-empty
   fields shall be further split as described in Field Splitting.


Now, of course POSIX doesn't say anything about arrays or the 
subarray/slice notation, but then Bash's reference mentions that
[@] and [*] are supposed to be analoguous to $@ and $*, and the 
description of ${parameter:offset:length} doesn't say that 
${array[@]:n:m} would act differently from ${array[@]} let alone 
differently from ${@:n:m}.

Instead, the wording of the subarray/slice expansion is similar for both 
${@:n:m} and ${array[@]:n:m}:

   ${parameter:offset:length}

   If parameter is ‘@’, the result is length positional parameters
   beginning at offset.

   If parameter is an indexed array name subscripted by ‘@’ or ‘*’, the
   result is the length members of the array beginning with
   ${parameter[offset]}.


It doesn't say what's done with those parameters or array members, but 
if the behaviour is supposed to be different between these two cases, 
it's not documented.


-- 
Ilkka Virta / itvirta@iki.fi

Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread


Thread

Re: Unquoted array slice ${a[@]:0} expands to just one word if IFS doesn't have a space Ilkka Virta <itvirta@iki.fi> - 2018-08-01 16:06 +0300

csiph-web