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

Re: test if a parameter is numeric



On Tue, Feb 20, 2007 at 08:52:26AM +0100, DervishD wrote:
[...]
>     [[ "$1" == ]]
> 
>     And the "[[" is NOT portable, either, it's a zsh thing (and probably
> bash has too, I don't know, but AFAIK is not POSIX/SuS).
[...]

rather from ksh.

POSIXly, you'd write

case $1 in
  ("" | *[!0-9]) echo not a number;;
  (*) echo is a number;;
esac

The equivalent of bash's [[ ... =~ ... ]]
is [[ ... -pcre-match ... ]] which uses perl compatible
regexps.

Note that zsh extended globbing patterns, though with a
different syntax are functionnaly equivalent to regexps and has
may additions over it.

ERE -> zsh
.      ?
*      #
+      ##
?      (|...)
(...)  (...)
(..|..)(..|..)

So

[[ $1 == [0-9]## ]]

-- 
Stéphane



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