bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Unquoted array slice address@hidden:0} expands to just one word if I


From: Ilkka Virta
Subject: Re: Unquoted array slice address@hidden:0} expands to just one word if IFS doesn't have a space
Date: Wed, 1 Aug 2018 16:06:03 +0300
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:52.0) Gecko/20100101 Thunderbird/52.9.1

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
address@hidden: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 address@hidden and [*] are supposed to be analoguous to $@ and $*, and the description of ${parameter:offset:length} doesn't say that address@hidden:n:m} would act differently from address@hidden let alone differently from ${@:n:m}.

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

  ${parameter:offset:length}

  If parameter is address@hidden, the result is length positional parameters
  beginning at offset.

  If parameter is an indexed array name subscripted by address@hidden 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 / address@hidden




reply via email to

[Prev in Thread] Current Thread [Next in Thread]