zsh-users
 help / color / mirror / code / Atom feed
From: Peng Yu <pengyu.ut@gmail.com>
To: zsh-users <zsh-users@zsh.org>
Subject: numeric for-loop and string for-loop
Date: Mon, 3 Dec 2018 07:56:26 -0600	[thread overview]
Message-ID: <CABrM6wnW1cap8rP8oW8rbRd00eByL86aQq0gXsn0mmg6Tirn0g@mail.gmail.com> (raw)

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

             reply	other threads:[~2018-12-03 13:57 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20181203135826epcas1p4dbee2d63b98245bd84c3179f644d2e6c@epcas1p4.samsung.com>
2018-12-03 13:56 ` Peng Yu [this message]
2018-12-03 14:48   ` Peter Stephenson
2018-12-03 14:53   ` Mikael Magnusson
2018-12-03 14:58     ` Mikael Magnusson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CABrM6wnW1cap8rP8oW8rbRd00eByL86aQq0gXsn0mmg6Tirn0g@mail.gmail.com \
    --to=pengyu.ut@gmail.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).