* numeric for-loop and string for-loop
@ 2018-12-03 13:56 ` Peng Yu
2018-12-03 14:48 ` Peter Stephenson
2018-12-03 14:53 ` Mikael Magnusson
0 siblings, 2 replies; 4+ messages in thread
From: Peng Yu @ 2018-12-03 13:56 UTC (permalink / raw)
To: zsh-users
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: numeric for-loop and string for-loop
2018-12-03 13:56 ` numeric for-loop and string for-loop Peng Yu
@ 2018-12-03 14:48 ` Peter Stephenson
2018-12-03 14:53 ` Mikael Magnusson
1 sibling, 0 replies; 4+ messages in thread
From: Peter Stephenson @ 2018-12-03 14:48 UTC (permalink / raw)
To: zsh-users
On Mon, 2018-12-03 at 07:56 -0600, Peng Yu wrote:
> To make the second i as string in zsh, what is the best solution so
> that the minimum amount of code is changed.
If you want to turn an integer into a string,
declare +i i
will work.
pws
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: numeric for-loop and string for-loop
2018-12-03 13:56 ` numeric for-loop and string for-loop Peng Yu
2018-12-03 14:48 ` Peter Stephenson
@ 2018-12-03 14:53 ` Mikael Magnusson
2018-12-03 14:58 ` Mikael Magnusson
1 sibling, 1 reply; 4+ messages in thread
From: Mikael Magnusson @ 2018-12-03 14:53 UTC (permalink / raw)
To: Peng Yu; +Cc: zsh-users
% a=5+5 b=2\*3
% typeset -i i
% for i in a b; do declare -p i; done; declare -p a b
typeset -i i=10
typeset -i i=6
typeset a=5+5
typeset b='2*3'
If this is not what you want, you can use unset i before each loop.
On 12/3/18, Peng Yu <pengyu.ut@gmail.com> wrote:
> 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
>
--
Mikael Magnusson
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: numeric for-loop and string for-loop
2018-12-03 14:53 ` Mikael Magnusson
@ 2018-12-03 14:58 ` Mikael Magnusson
0 siblings, 0 replies; 4+ messages in thread
From: Mikael Magnusson @ 2018-12-03 14:58 UTC (permalink / raw)
To: Peng Yu; +Cc: zsh-users
On 12/3/18, Mikael Magnusson <mikachu@gmail.com> wrote:
> % a=5+5 b=2\*3
> % typeset -i i
> % for i in a b; do declare -p i; done; declare -p a b
> typeset -i i=10
> typeset -i i=6
> typeset a=5+5
> typeset b='2*3'
>
> If this is not what you want, you can use unset i before each loop.
In fact, bash will act the same way as zsh if you say "typeset -i i",
the only difference is that zsh will create parameters as integers in
a mathematical context if they did not previously exist. In other
words, your zsh script will also "work" if you do "typeset i" before
the first loop, because then it already exists when you get to the
math loop.
--
Mikael Magnusson
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2018-12-03 14:59 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <CGME20181203135826epcas1p4dbee2d63b98245bd84c3179f644d2e6c@epcas1p4.samsung.com>
2018-12-03 13:56 ` numeric for-loop and string for-loop Peng Yu
2018-12-03 14:48 ` Peter Stephenson
2018-12-03 14:53 ` Mikael Magnusson
2018-12-03 14:58 ` Mikael Magnusson
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).