Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: numeric for-loop and string for-loop
- X-seq: zsh-users 23788
- From: Mikael Magnusson <mikachu@xxxxxxxxx>
- To: Peng Yu <pengyu.ut@xxxxxxxxx>
- Subject: Re: numeric for-loop and string for-loop
- Date: Mon, 3 Dec 2018 15:53:50 +0100
- Cc: zsh-users <zsh-users@xxxxxxx>
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=DRhNTcJQzjqZE1yLLD6c08IvYWYOqXe9beBHKElztCc=; b=ZTMLsy7m5/HgbxmV97vBvnWcFgXP1H3Y/rAlhO79AETRxJmas4Cudtgg3R40BMUfNc x1ePx1cYIqauvCsJdGbMae3RdLNe8FbotsbKVeUGLYcpfWkhhtvSNvoLz1A8/ZPnAUqN EygXEWy5S3iAyEestBjX2rLf2YW65/PuDzrgMMkdWOE/V+Dn+rhNYvGa+BX3pOZuHTUS p5CwUcg+hpOS95IKFfM1ebDoSaTNv5iZkzLKCsb4fdUkhk6XggGz9/W/+P2Sul5BJOb6 zChI5wwjNCQtxlyX71w9EmpQD9hn1vnZFXps44BMuW9IiI9AZNwRI9XJT0kQSMHYhNka WYaw==
- In-reply-to: <CABrM6wnW1cap8rP8oW8rbRd00eByL86aQq0gXsn0mmg6Tirn0g@mail.gmail.com>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- List-unsubscribe: <mailto:zsh-users-unsubscribe@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <CABrM6wnW1cap8rP8oW8rbRd00eByL86aQq0gXsn0mmg6Tirn0g@mail.gmail.com>
% 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@xxxxxxxxx> 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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author