Unix – Check that a variable is a number in UNIX shell

shellunix

How do I check to see if a variable is a number, or contains a number, in UNIX shell?

Best Solution

if echo $var | egrep -q '^[0-9]+$'; then
    # $var is a number
else
    # $var is not a number
fi