zsh: for loop over newline-delimited entries

To split on newline in zsh, you use the f parameter expansion flag (f for line feed) which is short for ps:\n::

for num (${(f)numholds}) print -r New item: $num

You could also use $IFS-splitting which in zsh (contrary to other Bourne-like shells) you have to ask for explicitly for parameter expansion using the $=param syntax ($= looks a bit like a pair of scissors):

for num ($=numholds) print -r New item: $num

Your variable is a string, not an array. That it happens to contain newlines isn't relevant. Your for num in $numholds sees the entire thing as a single value since that's how you have it stored.

The simple workaround would be to use a while loop and the <<< here-string construct to feed it:

% while read num; do echo "$num"; echo "New Item"; done <<<"$numholds"
409503
New Item
409929
New Item
409930
New Item
409932
New Item
409934
New Item
409936
New Item
409941
New Item
409942
New Item
409944
New Item
409946
New Item

This is essentially equivalent to the uglier (see here for why this is a bad idea):

% for num in $(echo "$numholds"); do echo $num; echo "new item"; done
409503
new item
409929
new item
409930
new item
409932
new item
409934
new item
409936
new item
409941
new item
409942
new item
409944
new item
409946
new item

Finally, you might want to make the variable an array instead:

numArray=($(echo "$numholds"))

You can now use the construct you wanted:

% for num in "${numArray[@]}"; do echo $num; echo "new item"; done                   
409503
new item
409929
new item
409930
new item
409932
new item
409934
new item
409936
new item
409941
new item
409942
new item
409944
new item
409946
new item
tpad% 

Tags:

Zsh

Array