On Mon, Feb 10, 2025 at 5:48 PM Chet Ramey <[email protected]> wrote
>
> There isn't a reward for brevity or obfuscation; say what you mean:
>
> isnum2()
> {
> case "$1" in
> [-+] | '') return 1;; # empty or bare `-' or `+'
> [-+]*[!0-9]*) return 1;; # non-digit with leading sign
> [-+]*) return 0;; # OK
> *[!0-9]*) return 1;; # non-digit
> *) return 0;; # OK
> esac
> }
>
> It obviously doesn't handle 0x constants, but could be changed to.
>
Would you accept this one then, should be general enough, faster too, and
explicit, really mean what is accepted as num literal
$ echo $BASH_VERSION
5.2.21(1)-release
$ function isanum
{ (($1)) || (($1==0)) && return 0
return 1
} >/dev/null 2>&1
$ isanum 0 && echo yes || echo no
yes
$ isanum 1 && echo yes || echo no
yes
$ isanum 010 && echo yes || echo no
yes
$ isanum -010 && echo yes || echo no
yes
$ isanum 0x10 && echo yes || echo no
yes
$ isanum +0x10 && echo yes || echo no
yes
$ isanum 64#I_like_this_one && echo yes || echo no
yes
$ isanum '' && echo yes || echo no
no
$ isanum '+' && echo yes || echo no
no
$ zero=0
$ one=1
$ isanum zero && echo yes || echo no
yes
$ isanum one && echo yes || echo no
yes
$ isanum '' && echo yes || echo no
no
$ isanum '+' && echo yes || echo no
$ bad=4#j
$ isanum bad && echo yes || echo no
no
Look good to me ...