I found a similar question here, but it does not seem to be identical, and none of the answers provide the output that I want. I have an array, for which I want to find how many elements it contains by accessing it using dynamically generated name.
declare -a array0=(2 4 2 5) # contains 4 values indx=0 Name="array$indx" # create a name reference => array0 # I know how to obtain an indexed value by INDIRECT reference: val0=${!Name[0]} # I also know how to get array length using DIRECT name len=${#array0[@]} What I need is find the number of elements of the array0 by referencing it using variable Name
len=${#!Name[@]} # the syntax is incorrect Any suggestions for implementation?
EDIT:
I was wrong about being able to access the array entries using this:
val0=${!Name[0]} It only works for indx=0, so if I wanted to get other entries from the array, it simply returns an empty string:
val4=${!Name[4]} #does not work or
i=4 val4=${!Name[$i]} # does not work