My POSIX is_integer () function looks like this for a long time:
#!/bin/sh
is_integer ()
{
[ "$1" -eq "$1" ] 2> /dev/null
}
However, today, I found it broken. If there are some spaces around the number, it surprisingly also evaluates to true, and I have no idea how to fix that.
Example of correct (expected) behavior:
is_integer 123 evaluates to true.
Example of incorrect (unexpected) behavior:
is_integer ' 123' also evaluates to true, however it obviously contains a leading space, thus the function is expected to evaluate to false in such cases.
POSIX-compliant suggestions only, please. Thank you.
-eqquite a lot wider than to just integers. Stuff likeabc(the value on variableabc),12.345(floating point),1+1(arithmetic expression) get accepted.