Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

numeric for-loop and string for-loop



Hi,

The following zsh and bash code generate different resutls. I feel the
bash convention is better.

To make the second i as string in zsh, what is the best solution so
that the minimum amount of code is changed. (I'd rather not to change
the second `i` to some other variable name as each for-loop should be
independent from each other, and should not know each other to make
the code work.). Thanks.

$ cat main.sh
#!/usr/bin/env zsh
# vim: set noexpandtab tabstop=2:

set -v
for ((i=0;i<2;++i))
do
    echo $i
done

declare -p i
for i in a b
do
    declare -p i
done


$ cat main.bash
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

set -v
for ((i=0;i<2;++i))
do
    echo $i
done

declare -p i
for i in a b
do
    declare -p i
done


$ ./main.sh
for ((i=0;i<2;++i))
do
    echo $i
done
0
1

declare -p i
typeset -i i=2
for i in a b
do
    declare -p i
done
typeset -i i=0
typeset -i i=0


$ ./main.bash
for ((i=0;i<2;++i))
do
    echo $i
done
0
1

declare -p i
declare -- i="2"
for i in a b
do
    declare -p i
done
declare -- i="a"
declare -- i="b"

-- 
Regards,
Peng



Messages sorted by: Reverse Date, Date, Thread, Author