Bash: integer expression expected, using read/test
Actually you can set some attributes on variables using the declare
(or the old typeset
) builtin. declare -i var1 var2
will set integer attribute on those variables. After that assignments which attempt to set non-integer values to those variables will raise error.
But your problem is with the syntax. When using a variable's value you have to prefix its name with $
:
if [ "$var1" -lt "$var2" ]; then
echo "$var1 is lt $var2"
else
echo "$var2 is lt $var1"
fi
Exceptions are the arithmetic evaluations, where is no need for the $
:
if ((var1<var2)); then
echo "$var1 is lt $var2"
else
echo "$var2 is lt $var1"
fi
As a word of warning, inside [
..]
always double quote your variables to avoid word expansion messing up your expression's syntax. (I mean, you will have problems with unset variables, variables containing empty string and variables containing IFS
characters.) Or you can use the newer and better [[
..]]
instead which handles such cases correctly:
if [[ $var1 -lt $var2 ]]; then
echo "$var1 is lt $var2"
else
echo "$var2 is lt $var1"
fi