What shell are you using? If it's bash, ksh, or another advanced shell, then you could do something like this instead:
Code:
if [[ ${number//[0-9]} ]]; then
echo "not an integer"
else
echo "an integer"
fi
${number//[0-9]} removes every digit from the string. If there's anything left over then it's a non-integer value and the test returns true.
Or here's a different way to do it that should work in all bourne-style shells, and is probably more efficient to boot:
Code:
case $number in
*[^0-9]*) echo "not an integer" ;;
*) echo "an integer" ;;
esac
In this one it globs the string for non-integer characters.
There are many other ways to go about this as well.
Code:
[ 5 -eq 05 ] is true, but
Numbers that begin with 0 are usually treated as octal values. Using values of 08, or 09 can give you funny results. Interestingly enough, the above pattern works just fine (when using 09) when I use "
[", but not when I use "
[[".
arithmetic expressions