Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 9 down vote accepted

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
share|improve this answer
    
Also, [[ $var1 < $var2 ]] is a string comparison, not an arithmetic comparison... [[ 11 > 2 ]] tests as false ... [[ 11 -gt 2 ]] tests as true –  Peter.O Jul 13 '12 at 17:24
    
@Peter.O, you are right. Since I use only arithmetic evaluation for numeric comparisons, I forgot that. Thank you. –  manatwork Jul 13 '12 at 17:27

You should reference variables prepending the name with $

if [ $var1 -lt $var2 ] ; then...
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.