Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Array expansion interacts with brace expansion in the wrong order



On Thu, 13 Jul 2017 21:42:50 -0400
Anders Kaseorg <andersk@xxxxxxx> wrote:
> The documentation for ${^spec} says
> 
> “Internally, each such expansion is converted into the equivalent list for 
> brace expansion. E.g., ${^var} becomes {$var[1],$var[2],...}, and is 
> processed as described in Brace Expansion below.”
> 
> That would seem to support my expectation that {$^a,4,5,6} should become 
> {{1,2,3},4,5,6}, which expands to 1 2 3 4 5 6.

What it means is the effect similar to brace expansion is *performed*
immediately, not simply recorded as happening below with the other brace
expansion.  So by the time the shell even looks to see if there's any
(actual) brace expansion to do, the array has already been expended
using its own similar rules and the shell is looking at:

{1,4,5,6} {2,4,5,6] {3,4,5,6}

There's no likelihood of the big rewrite that would be necessary to
transform the array expansion into such a form that it actually happens
with the brace expansion; indeed the brace expansion code is quite ad
hoc, as I rediscovered only yesterday.

The ordering of diferent sorts of expansion in the shell is always a big
source of confusion and in head scratching about how to get things to
work, but the key is to think of array expansion and brace expansion as
completely separate stages.  I can't think of an easy workaround for
your case except by constructing an array

b=($a 4 5 6)

which is obviously rather more verbose.

pws



Messages sorted by: Reverse Date, Date, Thread, Author