Reputation: 105
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!!!
Upvotes: 3
Views: 120
Reputation: 20980
Use this syntax:
name="arr[@]"
array=("${!name}")
Your other code is fine.
Or if you have this passed as a variable, name="arr"
You can always use this hack:
name_temp="$name[@]"
array=("${!name_temp}")
Upvotes: 3