Question

Recently I've got confused by the following situation. What is the difference between two if usage:

Case 1

amount=10  
if [[ $amount -eq 10 ]]   
then  
    echo "something"  
fi  

script output:

$ ./1.sh  
something  

Case 2

if [[ amount -eq 10 ]]   

This also works like this (note that the variable name doesn't contain the $).

So the question is how does it work even without dollar sign in the variable name.

P.S. I'm using a POSIX shell on HP-UX.

Was it helpful?

Solution

man bash

ARITHMETIC EVALUATION ... Shell variables are allowed as operands; parameter expansion is per‐ formed before the expression is evaluated. Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax.

In this context shell does not expect anything but numerics, so it expands strings as variables. That makes sense to me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top