0

Inside which type of parenthesis (), (()), {}, [[ ]], $( ), $(( )) should we use the dollar sign before the variable to expand it?

Caterina
  • 775
  • 9
  • 26

2 Answers2

2

The arithmetic expansion is the one that doesn't need dollar sings, which means both ((...)) and $((...)). But you don't need the dollar sign in other arithmetic contexts, either, e.g. in array indices.

arr=(a b c)
x=1
echo "${arr[x]}"  # b

See "Arithmetic Evaluation" in man bash.

choroba
  • 231,213
  • 25
  • 204
  • 289
1

It depends... Try it yourself in a bash.
A big Country is in array number 4

$ array=(0 1 2 3 COUNTRY 5 6 7 8 9)
$ echo ${array[4]}
COUNTRY

And now lets expand it...

$ array=(0 1 2 3 $COUNTRY 5 6 7 8 9)
$ echo ${array[4]}
DE

...to a very little country.

But what should it be?
My personnel like...

$ array=(0 1 2 3 ${COUNTRY} 5 6 7 8 9)
$ echo ${array[4]}
DE

...better because it makes it clearer that ${COUNTRY} is a Variable than a single Dollarsign ($).

koyaanisqatsi
  • 2,585
  • 2
  • 8
  • 15