Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: test if a parameter is numeric



On Fri, Feb 09, 2007 at 06:29:38PM -0800, Bart Schaefer wrote:
> On Feb 9,  5:05pm, zzapper wrote:
> }
> } I just want to test if a parameter is numeric
> } 
> } if [[ "$1" == [0-9] ]]
> 
> Better:
> 
>   if [[ "$1" = <-> ]]
> 
> This is a zsh-ism, so it's not cross-shell portable, but the pattern <->
> matches any string having any number of digits and only digits.

I was under the impression that zzapper was looking for
something to match *one* *decimal* digit though.

For positive decimal numbers, <-> will do (unless you don't want
to consider 09 or numbers greater than your max interger as
valid numbers).

To check for signed integer constants expressed in any notation
recognized by zsh (like 0xa, 8#77, 012 (being 10 or 12 depending
on "octalzeroes")...), it's a bit more tricky.

By the way, are those normal:

$ echo $((40#^))
39

(given that ^ is also an arithmetic operator)?

$ echo $((02#11))
3
$ setopt octalzeroes
$ echo $((02#11))
zsh: bad math expression: operator expected at `#11'
$

-- 
Stéphane



Messages sorted by: Reverse Date, Date, Thread, Author