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

Re: test if a parameter is numeric



On Sat, Feb 10, 2007 at 01:13:57PM -0500, Brian K. White wrote:
[...]
>    typeset -i N="$1" 2>/dev/null
> or if your shell has built in arithmatic (zsh/kash/bash, not plain sh on 
> platforms where sh isn't really bash) instead of using a type defined 
> variable, using the common age old trick (in any language) of just 
> attempting an arithmatic operation, add 0 to the value.
>    unset N
>    N=$((1+0))
> if $1 was numeric the N will have it, else N will still be unset
[...]

Those approaches will say that "1+1", "a=1", "SECONDS"... are
numbers some with potentially nasty side effects, as for $1 ==
"PATH=0", 

$ set 'PATH=0'
$ N=$(($1+0))
$ ls
ls:2: command not found: ls

-- 
Stéphane



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