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 / itvi...@iki.fi


Reply via email to