Bash: integer expression expected, using read/test

arithmeticbash

I'm learning the basics of shell programming, so I wrote something simple like

#!/bin/bash
read var1
read var2
if [ var1 -lt var2 ]; then
    echo "var1 is lt var2"
else
    echo "var2 is lt var1"
fi

My understanding is that variables are untyped in Bash, and that any variable may be used as an integer if it only contains digits. But I get the error "integer expression expected" when I run this… why?

Best Answer

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
Related Question