Quoting from the Bash manual:

An indexed array is created automatically if any variable is assigned to using the syntax


The subscript is treated as an arithmetic expression that must evaluate to a number.

Is subscript

  • an arithmetic expression, or
  • an arithmetic expansion where $(( and )) are ignored and only an arithmetic expression is there?

In other words, is there a step of arithmetic expansion here, just like when interpreting a command with arithmetic expansion?

For example,

$ declare -p myarr
declare -a myarr='([0]="0" [2]="3" [3]="9999")'

$ echo ${myarr[1+2]}
$ echo ${myarr[$((1+2))]}
  • 1
    Are you asking if your demonstration (the first echo command) is true?
    – Jeff Schaller
    May 3, 2016 at 16:22
  • No. I am asking the questions I asked. The example is to show that explicit arith expansion works.
    – Tim
    May 3, 2016 at 16:25
  • 1
    You ask if subscript is an arithmetic expansion, then show that it is. It seems self-answered?
    – Jeff Schaller
    May 3, 2016 at 16:26
  • 1
    Two ways that achieve the same thing are not necessarily the same.
    – Tim
    May 3, 2016 at 16:28

2 Answers 2


Is there a step of arithmetic expansion [with array subscripts], just like when interpreting a command with arithmetic expansion?


Variable expansion:

$ unset -v a
$ declare -p a
-bash: declare: a: not found
$ x=4
$ a[x]=4
$ declare -p a
declare -a a='([4]="4")'

Parameter expansion:

$ six=six
$ a[${#six}]=3
$ declare -p a
declare -a a='([3]="3" [4]="4")'

Command substitution:

$ a[$(echo 9)]=9
$ declare -p a
declare -a a='([3]="3" [4]="4" [9]="9")'

Quote removal:

$ a["5"]=5
$ declare -p a
declare -a a='([3]="3" [4]="4" [5]="5" [9]="9")'
$ a['6']=6
$ declare -p a
declare -a a='([3]="3" [4]="4" [5]="5" [6]="6" [9]="9")'
$ a[\7]=7
$ declare -p a
declare -a a='([3]="3" [4]="4" [5]="5" [6]="6" [7]="7" [9]="9")'


3.5.5 Arithmetic Expansion

All tokens in the expression undergo parameter and variable expansion, command substitution, and quote removal.

  • Is a subscript to an indexed array an arithmetic expansion?

To be precise: No, it is an "arithmetic expression", identical to what is inside an "arithmetic expansion" but lacks the $(( )).

Both evaluate the expression till it becomes a number (or a null).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.