Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

bash input:

if [[ 167 > 10800 ]]
then
    echo "I can't compare"
fi

bash output:

I can't compare

I guess the question is pretty obvious...

share|improve this question
    
I know the operator is -gt and -lt for [ and ]. I am not sure what [[ and ]] is. –  billc.cn Jul 21 '11 at 21:12
1  
    
@slhck: Neither of them works, actually. –  Mehrdad Jul 21 '11 at 21:19
    
@Mehrdad Neither of what? –  slhck Jul 21 '11 at 21:22
    
@slhck: Single or double brackets. –  Mehrdad Jul 21 '11 at 21:23

3 Answers 3

up vote 7 down vote accepted

To quote from the bash(1) manual page:

When used with [[, the < and > operators sort lexicographically using the current locale.

So 167 is indeed greater than 10800 as 6 is a greater ASCII character than 0.

share|improve this answer
    
OOOOHHHH facemonitor +1 –  Mehrdad Jul 21 '11 at 21:21

Use curved brackets for a true numeric comparison

if (( 167 > 10800 ))
then
    echo "I can't compare"
fi

Good summary here: http://fvue.nl/wiki/Bash:_Numeric_comparison

share|improve this answer

The > character doesn't work for the type of comparison you want. You have to use -gt:

if [[ 167 -gt 10800 ]]
then
    echo "I can't compare"
fi

And if you want to do a less-than comparison, you need to do -lt. To see what other options you need to do for comparison, look at the test manpage.

share|improve this answer
    
-1 I beg to differ. –  Mehrdad Jul 21 '11 at 21:18
    
@Mehrdad: edited. –  Wuffers Jul 21 '11 at 21:20
    
Interesting, +1... –  Mehrdad Jul 21 '11 at 21:22

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.