2

I'm trying to create this associative array, but it throws an error because of the volumeClaimTemplates[0]

This is my code:

declare -A example=(["'spec.statefulSet.spec.volumeClaimTemplates[0].spec'"]="TEST")

This is the error:

['spec.statefulSet.spec.volumeClaimTemplates[0].spec']=TEST: bad array subscript

If I remove the [0] from the volumeClaimTemplates then I get no errors.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • Try escaping the square brackets - i.e. `\[0\]`. – Rfroes87 Sep 10 '20 at 20:34
  • 2
    Can't reproduce in bash 5.0. – jordanm Sep 10 '20 at 20:35
  • 1
    Reproduces in `docker run bash:4.2`. I believe that in older bash versions array subscripts were restricted to some set of characters. @kooreshakhbari This is a question&answer forum. You asked no question in your post. In the spirit of this forum, could you add a question? Seems like `]` character is the problem - `declare -A a=(["]"]=1)` – KamilCuk Sep 10 '20 at 20:44
  • 3
    Are you trying to simulate nested objects using associative arrays? Maybe you should use a language that already has them. – Barmar Sep 10 '20 at 20:46
  • If I `printf %q\\n "'spec.statefulSet.spec.volumeClaimTemplates[0].spec'"` it outputs → `\'spec.statefulSet.spec.volumeClaimTemplates\[0\].spec\'`. I'd simply quote the associative array key the same way then: `declare -A example=([\'spec.statefulSet.spec.volumeClaimTemplates\[0\].spec\']="TEST")` – Léa Gris Sep 10 '20 at 22:10

1 Answers1

1

Got it to work like this

declare -A example=(["spec.statefulSet.spec.volumeClaimTemplates[0\].spec.storageClassName"]="TEST"