Skip to main content

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:

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
note: [ shortcut test, man test friend here.


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

Popular posts from this blog

How to change text Component easybook reloaded *newbee* - Joomla! Forum - community, help and support

After Effect warning: A problem occurred when processing OpenGL commands

Preconditions Failed. - Joomla! Forum - community, help and support