I would like to convert this Bash loop:
x="one two three"
for i in ${x}
do
echo ${i}
done
in such a way to work with both Bash and zsh
This solution works:
x=( one two three )
for i in ${x[@]}
do
echo ${i}
done
Anyway I am modifying x
from a string to an array.
Is there a way to loop over $x
in zsh when it is a string and in a way compatible with Bash?
I know about zsh setopt shwordsplit
to emulate Bash, but I can't set it ad hoc for the loop, because it would not work in Bash.
`
Best Answer
In zsh, this activates options that make it more compatible with ksh and bash, including
sh_word_split
. In other shells,emulate
doesn't exist so this does nothing.