Thread: Elegant way to test a variable if a number with SH / DASH ?
hello,
found several solutions not posix longer.
http://stackoverflow.com/questions/8...number-in-bash
simplest way be:
code:#!/bin/sh if [ $var ???? ] ; echo number else echo not_number fi
you can use tr delete digits, test whether resulting string empty:
note: [ shortcut test, man test friend here.code:firas@wakaba ~ % cat test.sh #!/bin/dash if [ $(echo $1 | tr -d '[:digit:]') ]; echo "not number" else echo "number" fi firas@wakaba ~ % ./test.sh foo not number firas@wakaba ~ % ./test.sh 1 number firas@wakaba ~ % ./test.sh 12345 number![]()
Forum The Ubuntu Forum Community Ubuntu Official Flavours Support New to Ubuntu [ubuntu] Elegant way to test a variable if a number with SH / DASH ?
Ubuntu
Comments
Post a Comment