zsh-users
 help / color / mirror / code / Atom feed
From: Peter Stephenson <Peter.Stephenson@csr.com>
To: <zsh-users@zsh.org>
Subject: Re: Strange behavior with "for i in .."
Date: Fri, 23 Sep 2011 11:41:59 +0100	[thread overview]
Message-ID: <20110923114159.6c7460ce@pwslap01u.europe.root.pri> (raw)
In-Reply-To: <4E7C5F9D.2070308@gmail.com>

On Fri, 23 Sep 2011 13:29:49 +0300
Volodya Khomchak <kolombo.inc@gmail.com> 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 <pws@csr.com>            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


  parent reply	other threads:[~2011-09-23 11:15 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-23 10:29 Volodya Khomchak
2011-09-23 10:41 ` Mikael Magnusson
2011-09-23 10:51   ` Volodya Khomchak
2011-09-23 10:41 ` Peter Stephenson [this message]
2011-09-23 10:43 ` Oliver Kiddle

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=20110923114159.6c7460ce@pwslap01u.europe.root.pri \
    --to=peter.stephenson@csr.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).