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...

link|improve this question

69% accept rate
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
show 1 more comment
feedback

3 Answers

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.

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

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.

link|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
feedback

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

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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