bashshell

How do I test if a variable is a number in Bash?


I just can't figure out how do I make sure an argument passed to my script is a number or not.

All I want to do is something like this:

test *isnumber* $1 && VAR=$1 || echo "need a number"

Any help?


Solution

  • One approach is to use a regular expression, like so:

    re='^[0-9]+$'
    if ! [[ $yournumber =~ $re ]] ; then
       echo "error: Not a number" >&2; exit 1
    fi
    

    If the value is not necessarily an integer, consider amending the regex appropriately; for instance:

    ^[0-9]+([.][0-9]+)?$
    

    ...or, to handle numbers with a sign:

    ^[+-]?[0-9]+([.][0-9]+)?$