Home > Back-end >  Inside which type of parenthesis should we use $ to expand a variable in bash
Inside which type of parenthesis should we use $ to expand a variable in bash

Time:10-05

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

CodePudding user response:

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.

  •  Tags:  
  • bash
  • Related