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, Bart Schaefer wrote:
> On Thu, Jul 13, 2017 at 3:45 PM, Anders Kaseorg <andersk@xxxxxxx> wrote:
> > Given a=(1 2 3), I would expect {$^a,4,5,6} to expand to 1 2 3 4 5 6, but
> > it doesn’t.  Instead the 4 5 6 is duplicated for every element of $^a
> 
> Your expectation is wrong.  Brace expansion happens last of all
> (except for globbing), so {$^a,4,5,6} is {1,4,5,6} {2,4,5,6}
> {3,4,5,6}.  This is documented.
> 
> Even if this happened in the order you expect, the way you've written
> it would have given 1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 5 3 6 because of the
> meaning of $^a.

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 you are after is simply {"$a",4,5,6}.

That produces 4 words ("1 2 3" 4 5 6), and doesn’t generalize (e.g. I 
would expect {$^a,4,5,6}{x,y} = {{1,2,3},4,5,6}{x,y} = 1x 1y 2x 2y 3x 3y 
4x 4y 5x 5y 6x 6y).

Anders



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