bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: find len of array w/name in another var...(bash 4.4.12)


From: Andreas Kusalananda Kähäri
Subject: Re: find len of array w/name in another var...(bash 4.4.12)
Date: Tue, 20 Oct 2020 10:29:40 +0200

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

[Prev in Thread] Current Thread [Next in Thread]