From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 28806 invoked by alias); 23 Sep 2011 11:15:25 -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: X-Seq: 16408 Received: (qmail 8729 invoked from network); 23 Sep 2011 11:15:14 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS autolearn=ham version=3.3.1 Received-SPF: none (ns1.primenet.com.au: domain at csr.com does not designate permitted sender hosts) Date: Fri, 23 Sep 2011 11:41:59 +0100 From: Peter Stephenson To: Subject: Re: Strange behavior with "for i in .." Message-ID: <20110923114159.6c7460ce@pwslap01u.europe.root.pri> In-Reply-To: <4E7C5F9D.2070308@gmail.com> References: <4E7C5F9D.2070308@gmail.com> Organization: Cambridge Silicon Radio X-Mailer: Claws Mail 3.7.9 (GTK+ 2.22.0; i386-redhat-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Originating-IP: [10.103.10.104] X-Scanned-By: MailControl A-12-01-02 (www.mailcontrol.com) on 10.71.0.126 On Fri, 23 Sep 2011 13:29:49 +0300 Volodya Khomchak wrote: > I faced with strange behavior with "for i in ..". > So the problem is next: > # for i in /etc/profile.d/*.sh;do echo $i;done > # zsh: bad math expression: operand expected at `/etc/profi...' > But if I change "i" to "file" it would work: > # for file in /etc/profile.d/*.sh;do echo $file;done > # /etc/profile.d/1.sh > # /etc/profile.d/2.sh > > zsh --version > zsh 4.3.10 (x86_64) > > So what is going on here ? Probably the first time i is referred to in the shell was in something like for (( i = 0; i < stuff; i++ )); ... or, in fact, any arithmetic involving "i". This has caused "i" to be implicitly typed as an integer. This only happens if i doesn't exist at that point. There are various things you can do; typeset i before you use i the first time will ensure it's a scalar (i.e. string) value even if you use it in arithmetic subsequently. Or, typeset +i i before you use it for any other purpose than arithmetic. Or, if the original use was in initialisation code and you don't want i to be exposed by it, simply unset i after that would be good enough. Or, of course, you can keep i for integers and use another variable for strings, which is in effect what you tried. -- Peter Stephenson Software Engineer Tel: +44 (0)1223 692070 Cambridge Silicon Radio Limited Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom More information can be found at www.csr.com. Follow CSR on Twitter at http://twitter.com/CSR_PLC and read our blog at www.csr.com/blog