a simple for loop in bash and zsh

Hi! I am just starting to learn scripting.
I am trying a simple script in bash and zsh
I have two questions:

First: Why zsh does not expand the var M? What I am doing wrong?

localhost galanom # bash -c 'M="m1 m2 m3 m4 m5"; for x in $M; do echo "<$x>"; done'
<m1>
<m2>
<m3>
<m4>
<m5>
localhost galanom # zsh -c 'M="m1 m2 m3 m4 m5"; for x in $M; do echo "<$x>"; done'
<m1 m2 m3 m4 m5>
localhost galanom # 

Second, if I use newlines or nothing after do statement, everything is ok. But if I use "do;" then:

localhost galanom # zsh -c 'M="m1 m2 m3 m4 m5"; for x in $M; do; echo "<$x>"; done'
<m1 m2 m3 m4 m5>
localhost galanom # bash -c 'M="m1 m2 m3 m4 m5"; for x in $M; do; echo "<$x>"; done'
bash: -c: line 0: syntax error near unexpected token `;'
bash: -c: line 0: `M="m1 m2 m3 m4 m5"; for x in $M; do; echo "<$x>"; done'
localhost galanom # 

If anyone can help me, I would appreciate it!
Thank you!

"Why zsh does not expand the var M? What I am doing wrong?"

Zsh supports word splitting but the option is not turned on by default.

zsh --shwordsplit -c 'M="m1 m2 m3 m4 m5"; for x in $M; do echo "<$x>"; done'

#or

zsh -yc 'M="m1 m2 m3 m4 m5"; for x in $M; do echo "<$x>"; done' 
1 Like