2

Say I have an array arr and an index x. How do I assign something to the array at index x+1? I'm getting bugs by trying the following, if one of them is correct I'd love to know which one and if not what am I doing wrong?

arr[$x+1]="hi"      # Doesn't work
arr[$((x+1))]="hi"  # Nope
Dori
  • 1,035
  • 1
  • 12
  • 22
  • 1
    Both work correctly. GNU Bash 4.2.37(1). – Dmitry Alexandrov Dec 24 '13 at 15:12
  • References: The [Bash guide for beginners](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_10_02.html) says an array index "is treated as an arithmetic expression that must evaluate to a positive number." [Arithmetic expressions](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html#sect_03_04_05) are whatever can go inside `$(( ))`. – cxw May 19 '18 at 12:53

1 Answers1

3

Almost there.

arr[(($x+1))]="hi"
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358