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


Groups > gnu.bash.bug > #14431 > unrolled thread

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

Started byIlkka Virta <itvirta@iki.fi>
First post2018-08-01 16:06 +0300
Last post2018-08-01 16:06 +0300
Articles 1 — 1 participant

Back to article view | Back to gnu.bash.bug

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  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

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

FromIlkka Virta <itvirta@iki.fi>
Date2018-08-01 16:06 +0300
SubjectRe: Unquoted array slice ${a[@]:0} expands to just one word if IFS doesn't have a space
Message-ID<mailman.4550.1533128777.1292.bug-bash@gnu.org>
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

[toc] | [standalone]


Back to top | Article view | gnu.bash.bug


csiph-web