From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from primenet.com.au (ns1.primenet.com.au [203.24.36.2]) by inbox.vuxu.org (OpenSMTPD) with ESMTP id 5e6e7649 for ; Mon, 3 Dec 2018 14:54:07 +0000 (UTC) Received: (qmail 7104 invoked by alias); 3 Dec 2018 14:53:55 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: List-Unsubscribe: X-Seq: 23788 Received: (qmail 10141 invoked by uid 1010); 3 Dec 2018 14:53:55 -0000 X-Qmail-Scanner-Diagnostics: from mail-io1-f48.google.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.100.2/25112. spamassassin: 3.4.2. Clear:RC:0(209.85.166.48):SA:0(-2.0/5.0):. Processed in 1.75345 secs); 03 Dec 2018 14:53:55 -0000 X-Envelope-From: mikachu@gmail.com X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | 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== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=DRhNTcJQzjqZE1yLLD6c08IvYWYOqXe9beBHKElztCc=; b=P2d5QKzBYe0Z/GovHhBMWVzPNKHBkoRsFIl4q/CBhpR5CXi8ErpeYM2Tl/Qv4GzJap qW0nFzrANUsKU52GF14ApmQQndilqPXFparUunLasMYMyX801bn0zIqSZXHHPso1QEgB 9kZ2FajAihpTm7WIwyJUeHpVrsSOXRpomS34EWPt5rrG8kD84tdr7td5qwICzFI7SBXw QJQJ7zut6ahiA1RW/yj4h6Mj6cvKoGiVbwhyZGoQ/rXJvTl+2D6dauwbhWSD2P4M365O mN7AcvIayVOTN8bo0pBFrXqTHouS28+Vnexuv7fzUqYhipnEJtzWh+pyKVMYGpcCI0Bf KcKQ== X-Gm-Message-State: AA+aEWZpxr32WZ1McOC/MdTb3o90xcWBQ/pFZ3N8kKg3KsSagokk0Wyw ufyCvbnljoFHDxKd1M02sRitMOnO7tKWBNrHVGgmgg== X-Google-Smtp-Source: AFSGD/XACTKfZeTT+TcxKPBH9qCusTrisE4mxuOzp3Ka6AnsIpUq/06evMhmgV/xyyWMcq3naI6bywvjvn5557m/Fig= X-Received: by 2002:a5e:8618:: with SMTP id z24mr5324952ioj.35.1543848831330; Mon, 03 Dec 2018 06:53:51 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: From: Mikael Magnusson Date: Mon, 3 Dec 2018 15:53:50 +0100 Message-ID: Subject: Re: numeric for-loop and string for-loop To: Peng Yu Cc: zsh-users Content-Type: text/plain; charset="UTF-8" % 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 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