Testing if a variable is an integer [duplicate]
As long as you're using bash version >=3 you can use a regular expression:
[[ $a =~ ^-?[0-9]+$ ]] && echo integer
While this bash FAQ mentions inconsistencies in the bash regex implementation in various bash 3.x (should the regex be quoted or not), I think in this case, there are no characters that need quoting in any version, so we are safe. At least it works for me in:
- 3.00.15(1)-release (x86_64-redhat-linux-gnu)
- 3.2.48(1)-release (x86_64-apple-darwin12)
- 4.2.25(1)-release (x86_64-pc-linux-gnu)
$ a=""$ [[ $a =~ ^-?[0-9]+$ ]] && echo integer$ a=" "$ [[ $a =~ ^-?[0-9]+$ ]] && echo integer$ a="a"$ [[ $a =~ ^-?[0-9]+$ ]] && echo integer$ a='hello world!'$ [[ $a =~ ^-?[0-9]+$ ]] && echo integer$ a='hello world 42!'$ [[ $a =~ ^-?[0-9]+$ ]] && echo integer$ a="42"$ [[ $a =~ ^-?[0-9]+$ ]] && echo integerinteger$ a="42.1"$ [[ $a =~ ^-?[0-9]+$ ]] && echo integer$ a="-42"$ [[ $a =~ ^-?[0-9]+$ ]] && echo integerinteger$ a="two"$ [[ $a =~ ^-?[0-9]+$ ]] && echo integer
A hackish-but-portable solution is to compare the value with itself using test
's -eq
(integer equality) operator and throw away any resulting error message:
is_int () { test "$@" -eq "$@" 2> /dev/null; }for input in "0.3" "abc" "3"; do if is_int "$input"; then echo "Integer: $input" else echo "Not an integer: $input" fidone
I was needing something that would return true only for positive integers (and fail for the empty string). I settled on this:
test -n "$1" -a "$1" -ge 0 2>/dev/null
the 2>/dev/null
is there because test prints an error (and returns 2) if an input (to -ge) doesn't parse as an integer
I wish it could be shorter, but "test" doesn't seem to have a "quiet" option and treats "" as a valid integer (zero).