Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: test if a parameter is numeric
- X-seq: zsh-users 11184
- From: Stephane Chazelas <Stephane_Chazelas@xxxxxxxx>
- To: zsh-users@xxxxxxxxxx
- Subject: Re: test if a parameter is numeric
- Date: Sat, 10 Feb 2007 08:35:11 +0000
- In-reply-to: <070209182939.ZM18654@xxxxxxxxxxxxxxxxxxxxxx>
- Mail-followup-to: zsh-users@xxxxxxxxxx
- Mailing-list: contact zsh-users-help@xxxxxxxxxx; run by ezmlm
- References: <Xns98D2ADD16C100zzappergmailcom@xxxxxxxxxxx> <070209182939.ZM18654@xxxxxxxxxxxxxxxxxxxxxx>
- Sender: Stephane Chazelas <stephane_chazelas@xxxxxxxx>
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