Reputation: 8081
I've seen other answers about bash integer checks and comparisons, however the results I'm getting are very confusing to me. Suppose I have this script:
if [[ $1 -eq $1 ]] ;then
echo "number"
else
echo "not number"
fi
if (( $1 >= 0 )) ;then
echo "number"
else
echo "not number"
fi
If I pass a string for parameter one , I get back "number".
Upvotes: 0
Views: 101
Reputation: 241908
That's because string is understood as a variable whose value is 0, and 0 >= 0
is true. Try with >
(but it will report 0 as not number
- but it already misclassifies all negative integers).
Cf:
a=1
b=a
x=b
(( x > 0 )) && echo 1
a=0
(( x > 0 )) || echo 0
or even
$ a=x
$ x=a
$ (( x > 0 ))
bash: ((: a: expression recursion level exceeded (error token is "a")
Bash tries hard to resolve the variable:
(
for l in a{1..1023} ; do
printf "$l\n$l="
done
echo 1
echo '((a1>0))'
) | tail -n+2 | bash
Upvotes: 2