How To Find BASH Shell Array Length ( number of elements )

by Vivek Gite · 5 comments

Q. How do I define array in a bash shell script? How do I find out bash array length (number of elements) while running a script using for shell loop?

A. Bash provides one-dimensional array variables. Any variable may be used as an array; the declare builtin will explicitly declare an array. There is no maximum limit on the size of an array, nor any requirement that members be indexed or assigned contiguously.

How do I define bash array?

Array can be defined using following syntax:
ArrayName=("element 1" "element 2" "element 3")
Define array called distro with 3 elements, enter:

distro=("redhat" "debian" "gentoo")

How do I reference any element in bash array?

Any element of an array may be referenced using following syntax:

${ArrayName[subscript]}

To print redhat i.e first element enter:

echo ${distro[0]}
echo ${distro[2]}  # will print gentoo

How do I find out bash shell array length?

You can easily find out bash shell array length using following syntax:

${#ArrayName[@]}

To print distro array length enter:

echo ${#distro[@]} 

Sample output:

3

If subscript is @ or *, the word expands to all members of name. By prefixing # to variable you will find length of an array (i.e number of elements).

Putting it all together

A sample shell script to print array called NAMESERVERS:

#!/bin/bash
# define array
# name server names FQDN
NAMESERVERS=("ns1.nixcraft.net." "ns2.nixcraft.net." "ns3.nixcraft.net.")
 
# get length of an array
tLen=${#NAMESERVERS[@]}
 
# use for loop read all nameservers
for (( i=0; i<${tLen}; i++ ));
do
  echo ${NAMESERVERS[$i]}
done

Sample output:

ns1.nixcraft.net.
ns2.nixcraft.net.
ns3.nixcraft.net.

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 5 comments… read them below or add one }

1 S. Nilesh 07.27.08 at 4:47 pm

Nice one ;)

2 Diya 07.28.08 at 8:20 am

Excellent info.

3 those 08.05.08 at 8:04 pm

how to clean the array? ;d

4 budacsik 09.20.08 at 12:18 pm

Hi!
I think that this example is bad.
I would do this without a block .

#!/bin/bash

NAMESERVERS=”ns1.nixcraft.net. ns2.nixcraft.net. ns3.nixcraft.net.”
for i in $NAMESERVERS; do
echo “$i”
done

5 Raymond 02.07.09 at 10:11 am

Problems,

a[2]=222
echo ${#a[@]}
1

Thus this is the lenght of the array, not the index of the last element.

Try instead:
for i in “${a[@]}”; do echo $i; done

Some further ideas may be found at
http://tldp.org/LDP/abs/html/arrays.html
http://www.linuxjournal.com/content/bash-arrays

Best Regards,
Raymond

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous FAQ:

Next FAQ:

nixCraft FAQ PDF Collection Now Available To All