4

When I test a script bellow:

#!/bin/bash

num1=100
num2=100


if test $num1 -eq $num2  # there is no difference using $num1 ,$[num1] and ${num1}
then 
    echo "equal"    
else
    echo "not equal"    
fi  

I know if we echo the variable, we should use the {} to define the periphery, but how about the []? Does [] some difference with {}?

1243916142
  • 365
  • 6
  • 17

1 Answers1

4

$x and ${x} have the same semantics, the braces are never needed, but they might be convenient when you need to separate the variable from a string following it:

var=program
echo "${var}s"  # $vars wouldn't work

$[x] is an old way of writing $((x)), i.e. result of an arithmetic expression.

x=3
y=2
echo $(( x + y ))  # 5

For more details, see Why should I use $[ EXPR ] instead of $(( EXPR ))?.

choroba
  • 231,213
  • 25
  • 204
  • 289