Nick Henry

Just a site to share some of my memories.

Testing to see if a variable is an integer

11 October 2010

Bash unfortunately does not include a built-in function to test if a variable is an integer. There is however a simple pattern matching hack that you can use to test this on your own:

Check for Unsigned Integer

[[ $number =~ ^[0-9]+$ ]] && echo "match found : integer"

Check for Signed Integer

[[ $number =~ ^[+-]?[0-9]+$ ]] && echo "match found : (signed) integer"