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

Re: Odd expansion behavior



On Thu, 03 Jul 2014 00:43:23 -0700
Dima Kogan <dima@xxxxxxxxxxxxxxx> wrote:
> I'm observing something odd. It might be a bug, but I don't understand
> it well-enough to call it that.
> 
> I see this:
> 
> $ x=(1 2 3); echo xxx${^x}
> xxx1 xxx2 xxx3
> 
> This output makes sense. However something slightly different is strange:
> 
> $ x=(1 2 3); y=xxx${^x}; echo $y
> xxx1 2 3
> 
> I asked on #zsh and osse helpfully pointed out that this can be made to
> work by expressing the assignment as 'y=(xxx${^x})' instead, which does
> work.

That's the difference between a scalar and an array assignment.  In a
scalar assignment, arrays are forced immediately to scalars, i.e. single
strings joined (by default) with spaces.  So the $x gets turned
immediately into "1 2 3"; the "^" has no special effect because you're
just joining that string with the "xxx".

It's a little bit like (but not exactly the same as) putting double
quotes around the expression.

It might throw a little bit of extra light on this if you look at the
words that are coming out; you can do this with "print -l" which outputs
one word per line.  So in the first case and after array assignment you
get

xxx1
xxx2
xxx3

because you are getting three different words from ${^x}, each one
joined to xxx, while after the scalar expansion you get

xxx1 2 3

because you have a single word.

pws



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