zsh-users
 help / color / mirror / code / Atom feed
* for x in * evaluates filenames as arithmetic expressions?
@ 2005-05-24 10:36 Einar Huginson
  2005-05-24 10:51 ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: Einar Huginson @ 2005-05-24 10:36 UTC (permalink / raw)
  To: zsh-users

When executed in a directory containing files with names such as

	2004-10-22.txt
	2004-10-29.txt
	...

a ZSH command of

	for x in *
	do
		echo $x
	done

fails with

	zsh: bad math expression: operator expected at `txt'

Thus, it appears that the wildcard is erroneously evaluated as arithmetic 
expression. This is confirmed by

	for x in 9-5
	do
		echo $x
	done

, which outputs
	
	4

This behavior differs significantly from that of other shells. Is there any 
way to turn it off? So far, the only way I have found to process such 
directories is to use BASH instead of ZSH.

Any hints are appreciated,
  Andreas Koch


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: for x in * evaluates filenames as arithmetic expressions?
  2005-05-24 10:36 for x in * evaluates filenames as arithmetic expressions? Einar Huginson
@ 2005-05-24 10:51 ` Peter Stephenson
  2005-05-24 11:21   ` Einar Huginson
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 2005-05-24 10:51 UTC (permalink / raw)
  To: zsh-users

Einar Huginson wrote:
> When executed in a directory containing files with names such as
> 
> 	2004-10-22.txt
> 	2004-10-29.txt
> 	...
> 
> a ZSH command of
> 
> 	for x in *
> 	do
> 		echo $x
> 	done
> 
> fails with
> 
> 	zsh: bad math expression: operator expected at `txt'

This means that the shell thinks x is an integer variable.  Check this
with

  print ${(t)x}

Use

  typeset +i x

to revert it to a scalar.  On the command line, simply "unset x" would
be good enough.

Obviously, you'll have to check what you've been doing to see why
it would have become an integer.  It certainly wouldn't do that because
of a "for" loop.  If you've still got the old command list associated
with x we can probably work it out.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070



**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

**********************************************************************


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: for x in * evaluates filenames as arithmetic expressions?
  2005-05-24 10:51 ` Peter Stephenson
@ 2005-05-24 11:21   ` Einar Huginson
  2005-05-24 11:36     ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: Einar Huginson @ 2005-05-24 11:21 UTC (permalink / raw)
  To: Peter Stephenson, zsh-users

Yes, that was indeed the case. I used `x' in an arithmetic for-loop earlier. 
However, I didn't realize that this would implicitly impose a `sticky' type 
for the variable. Now that I know, I'll be more careful in the future.

Many thanks for the instant reply,
  Andreas

On Tuesday 24 May 2005 12:51, you wrote:
> Einar Huginson wrote:
> > When executed in a directory containing files with names such as
> >
> > 	2004-10-22.txt
> > 	2004-10-29.txt
> > 	...
> >
> > a ZSH command of
> >
> > 	for x in *
> > 	do
> > 		echo $x
> > 	done
> >
> > fails with
> >
> > 	zsh: bad math expression: operator expected at `txt'
>
> This means that the shell thinks x is an integer variable.  Check this
> with
>
>   print ${(t)x}
>
> Use
>
>   typeset +i x
>
> to revert it to a scalar.  On the command line, simply "unset x" would
> be good enough.
>
> Obviously, you'll have to check what you've been doing to see why
> it would have become an integer.  It certainly wouldn't do that because
> of a "for" loop.  If you've still got the old command list associated
> with x we can probably work it out.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: for x in * evaluates filenames as arithmetic expressions?
  2005-05-24 11:21   ` Einar Huginson
@ 2005-05-24 11:36     ` Peter Stephenson
  0 siblings, 0 replies; 4+ messages in thread
From: Peter Stephenson @ 2005-05-24 11:36 UTC (permalink / raw)
  To: zsh-users

Einar Huginson wrote:
> Yes, that was indeed the case. I used `x' in an arithmetic for-loop earlier. 
> However, I didn't realize that this would implicitly impose a `sticky' type 
> for the variable. Now that I know, I'll be more careful in the future.

It's documented in the ARITHMETIC EVALUATION manual entry in zshmisc,
but fairly well down, so I don't suppose most people have read that far.
I expect the structure of the entry could be improved.

       If a variable is first assigned in a numeric context without previously
       being declared, it will be implicitly typed as  integer  or  float  and
       retain  that  type either until the type is explicitly changed or until
       the end of the scope.

The corollary is that if you always explicitly say "typeset x" before x
is ever used the variable will stay as a scalar.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

**********************************************************************


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2005-05-24 11:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-05-24 10:36 for x in * evaluates filenames as arithmetic expressions? Einar Huginson
2005-05-24 10:51 ` Peter Stephenson
2005-05-24 11:21   ` Einar Huginson
2005-05-24 11:36     ` Peter Stephenson

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).