I admit the function was mis-named, should be isanumexpr

As @chet noted this attempt was not too bad just need a little $1 check
before pursuing the idea of letting the bash scanner do the job for
complicated valid number (any base)

I come up with this one

function isanum
{ [[ "$1" =~ ^[+-]?[0-9][0-9a-zA-Z#@_]*$ ]] || return 1
  (($1)) || (($1==0))  && return 0
  return 1
}  >/dev/null 2>&1

$ isanum  1+1  && echo y || echo n
n
$ isanum  'i[$( date >&2 )]'  && echo y || echo n
n
$ isanum  64#yo  && echo y || echo n
y
$ isanum  -1  && echo y || echo n
y
$ isanum  yo  && echo y || echo n
n
$ isanum  ''  && echo y || echo n
n
$ isanum  0  && echo y || echo n
y

This time the 1st =~ do accept only number characters at large, the
complexity of deciding which [:letter:]@_ is ledgit for a given base is
differed to $(())

Getting closer to the perfect isanum() :-)

Note the I have a full one based only on regex =~ it is real ugly so that's
why I prefere de let $(()) do the scan, specially if one day it accept 0b
as base (unlikely but who knows. This code would still work.

Again modern bash.

Note too that I use function redirection because I got no idea how to nuke
$(( )) syntax error message

Reply via email to