Let's assume I have an array

> words=(foo bar baz)

Now I can join the elements

> echo ${(j., .)words}
foo, bar, baz

And I can append a string to the elements:

> echo ${^words}yeah
fooyeah baryeah bazyeah

With the following, I can append and join the elements:

> wordsyeah=(${^words}yeah)
> echo ${(j., .)wordsyeah}
fooyeah, baryeah, bazyeah

Is it possible to do print "fooyeah, baryeah, bazyeah" in a single expression, i.e. without using additional variables?

Bonus: Can I print that without using any variables at all?

As far as I can tell, this boils down to running parameter expansion on strings, but I wasn't able to find out how (or if) that is possible.

  • This is essentially a duplicate of this question on SO. As per my answer there you could achive this with echo ${(j:, :):-${^${=:-foo bar baz}}yeah}, which is actually harder to type and 12 characters longer than echo fooyeah, baryeah, bazyeah. So it mainly makes sense if variables are involved, for example echo ${(j:, :):-${^${words}}yeah} (with words being an array).
    – Adaephon
    Jul 8, 2016 at 20:47
  • Wow, you're totally right. I couldn't find that question. Now I'm wondering why I posted this here, too. And I was writing the answer just as you commented. I'll let the mods decide what to do with this question now. Jul 8, 2016 at 20:51

1 Answer 1


The answer is

> print ${(j., .)${:-${^words}yeah}}
fooyeah, baryeah, bazyeah


> print ${(j., .)${:-{foo,bar,baz}yeah}}
fooyeah, baryeah, bazyeah

without using variables at all

The critical part is ${name:-word}. It is explained in the manual.


If name is set, or in the second form is non-null, then substitute its value; otherwise substitute word. In the second form name may be omitted, in which case word is always substituted.

Thanks to phy1729 from #zsh for pointing this out to me :)

  • 1
    Can you please accept your own answer, so this question doesn't stay marked as "unanswered"? Thanks! Nov 8, 2021 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.