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.
#!/bin/bash

VALUE=10

if [[ VALUE -eq 10 ]]
then
    echo "Yes"
fi

To my surprise, this outputs "Yes". I would have expected it to require [[ $VALUE -eq 10 ]]. I've scanned the CONDITIONAL EXPRESSIONS section of man bash, but I did not find anything to explain this behaviour.

share|improve this question
add comment

1 Answer

up vote 11 down vote accepted

[[ is bash reserved word, therefore special expansion rules such as arithmetic expansion are applied, not like in case with [. Also arithmetic binary operator -eq is used. Therefore shell looks for integer expression and if text is found at the first item it tries to expand it as parameter. It is called arithmetic expansion and is present in man bash.

RESERVED WORDS
       Reserved words are words that have a special meaning to the shell.  
       The following words are recognized as reserved 
       …
       [[ ]]

[[ expression ]]
       Return  a  status  of 0 or 1 depending on the evaluation of 
       the conditional expression expression.  Expressions are 
       composed of the primaries described below under CONDITIONAL 
       EXPRESSIONS.  Word splitting and pathname expansion are not 
       performed on the words between the  [[  and  ]];  tilde 
       expansion, parameter and variable expansion, >>>_arithmetic 
       expansion_<<<, command substitution, process substitution, and 
       quote removal are performed.  

Arithmetic Expansion
       …
       The evaluation is performed according to the rules listed below 
       under ARITHMETIC EVALUATION.

ARITHMETIC EVALUATION
       …
       Within an expression, shell variables may also be referenced 
       by name without using the parameter expansion syntax.

So for example:

[[ hdjakshdka -eq fkshdfwuefy ]]

will return always true

But this one will return error

$ [[ 1235hsdkjfh -eq 81749hfjsdkhf ]]
-bash: [[: 1235hsdkjfh: value too great for base (error token is "1235hsdkjfh")

Also recursion is available:

$ VALUE=VALUE ; [[ VALUE -eq 12 ]]
-bash: [[: VALUE: expression recursion level exceeded (error token is "VALUE")
share|improve this answer
    
Why does being a reserved word mean it does arithmetic evaluation? I can't find that documented anywhere –  Mikel May 25 '12 at 19:47
    
Ah, here it is. gnu.org/software/bash/manual/… –  Mikel May 25 '12 at 19:58
    
I've included citation from man bash to my answer to make it clear. –  rush May 25 '12 at 20:01
    
@Mikel It's not directly the fact that [[ is a reserved word, but because what's within [[ … ]] is not ordinary command syntax, but a conditional expression. In a conditional expression, the arguments to arithmetic operators such as -eq are subject to arithmetic evaluation. –  Gilles May 25 '12 at 23:49
    
Yes. I was saying the first sentence gives that idea, which is misleading –  Mikel May 26 '12 at 5:27
add comment

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.