Skip to main content
Commonmark migration
Source Link

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

Yes.

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")'

Reference

3.5.5 Arithmetic Expansion

 

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

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

Yes.

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")'

Reference

3.5.5 Arithmetic Expansion

 

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

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

Yes.

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")'

Reference

3.5.5 Arithmetic Expansion

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

Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 264

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

Yes.

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")'

Reference

3.5.5 Arithmetic Expansion

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