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

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

On Fri, 14 Jul 2017 09:48:31 +0100
Peter Stephenson <p.stephenson@xxxxxxxxxxx> wrote:
> 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.

Hmm...  we're now in zsh-users territory, so I'll summarise.

The problem is that

a=(1 2 3)
print {$^a,4,5,6}

doesn't expand $^a at the same time as the braces, because all array
expansion comes before all brace expansion.  So how do you get 1 2 3 4 5
6 treated as a set of array elements without actually constructing an

Because of expansion ordering, the trick is to make sure it looks purely
like an array expansion --- if you leave any of it to brace expansion,
it'll expand recursively.

If you're not too fussy about embedded spaces, you can do

% print x${^=:-$a 4 5 6}y
x1y x2y x3y x4y x5y x6y

(x and y demonstrate the fact that we have indeed got 6 different

All the squiggles are necessary:

- ${...} creates the array expansion we're going to use to simulate
  brace expansion.
- ^ ensures that array expansion behaves like brace expansion.
- = splits the string "$a 4 5 6" on spaces.  Things will go awry here
  if there are embedded spaces.
- :- says "use the following if there's no parameter string yet" which
  there isn't.
- Then follows a simple string to be split.

I haven't come up with a way that allows you effectively to extend the
array without using the hack of splitting up strings.


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