Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I did in Zsh:

array={geometry, analysis, topology, graph theory, calculus}
echo $array

and then I wanted to add word "math:" to each element like" math:calculus":

while (( i++ < 10)); { echo math:$array[i] }

But it does not work? Why?

share|improve this question
um, are you sure this is ZSH? That doesn't look like the correct list initialization syntax to me... – jj33 Jun 15 '09 at 19:19

Works fine for me in zsh with the assignment changed from:

array={geometry, analysis, topology, graph theory, calculus}


array=(geometry, analysis, topology, graph theory, calculus)

But zsh has tons of options that change its behavior. Maybe the output 'setopt' might help.

share|improve this answer
Also , make sure you reinitialize i to 0 each time you run it – Kyle Brandt Jun 15 '09 at 19:38

Just do:

array=(geometry analysis topology "graph theory" calculus)
print -l math:${^array}

or check RC_EXPAND_PARAM for the ${^var} form.

share|improve this answer

Welp, I'm going to go out on a limb here (because I don't accept that the supporting code is correct) and say that "echo math:$array[i]" is missing a dollar sign and should be "echo math:$array[$i]"

share|improve this answer
With mine the i doesn't need the dollar sign. – Kyle Brandt Jun 15 '09 at 19:41
well, at least I was right about the initialization being wonky. The ZSH example I looked at used the dollar sigil when referencing the loop variable. – jj33 Jun 15 '09 at 19:44

Iterating through an array works better with for because you won't over run the end like your code will (unless you set your limit to the size of the array with ${#array[*]}).

Also, I assume you don't want the commas to be included as part of the strings and you should use parentheses instead of curly braces for your array.

array=(geometry analysis topology "graph theory" calculus)
for i in $array; do echo math:$i; done
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.