I have such bash code below, I just want to know how to find an array by it's name:
#!/bin/bash
arr=("object1" "object2")
name="arr"
array=${!name}
echo object0 = ${array[0]}
echo object1 = ${array[1]}
outputs below:
object0 = object1
object1 =
I'm wondering why I cannot index the second element and how can I do that!!!
$arrwhenarris an array yields the zeroth element of the array (only). So the assignmentarray=${!name}assigns a single value toarray, and then you can't pick up multiple values from it.