On Mon, Feb 10, 2025, at 2:24 PM, Phi Debian wrote: > On Mon, Feb 10, 2025 at 5:48 PM Chet Ramey <chet.ra...@case.edu> 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
This accepts many valid expressions, not just literals. % cat /tmp/isnum.bash chet() { case $1 in [-+] | '') return 1 ;; [-+]*[!0-9]*) return 1 ;; [-+]*) return 0 ;; *[!0-9]*) return 1 ;; *) return 0 ;; esac } phi() { (($1)) || (($1==0)) } chet '1+2' printf 'chet 1+2 -> %s\n' "$?" phi '1+2' printf 'phi 1+2 -> %s\n' "$?" % bash /tmp/isnum.bash chet 1+2 -> 1 phi 1+2 -> 0 -- vq