On Tue, Oct 20, 2020 at 12:58:36AM -0700, L A Walsh wrote:
> There's got to be an easier way to do this, but not remembering or finding
> it:
> 
> First tried the obvious:
> declare -a ar1=([0]="1" [1]="2" [2]="3" [3]="44")
> an=ar1
> echo ${#!an[@]}
> -bash: ${#!an[@]}: bad substitution
> 
> This works but feels kludgy
> 
> an=ar1
> eval echo \${#$an[@]}
> 4
> 
> ----
> I thought the !name was supposed to take the place
> of using $an, but haven't seen a case where !an works where
> an points to an array name.
> 
> Is there a place in the bash manpage that gives an example of using !name
> where name points to an array?
> 
> Thanks...
> -l
> 
> 


In bash 4.3+, I would manke your "ar" variable a name reference variable
instead:

$ ar1=(1 2 3 44)
$ declare -n ar=ar1
$ echo "${#ar[@]}"
4


-- 
Andreas (Kusalananda) Kähäri
SciLifeLab, NBIS, ICM
Uppsala University, Sweden

.

Reply via email to