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

How do I define an 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?

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. This page shows how to find number of elements in bash array.

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:


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:


To print distro array length enter:

echo "${#distro[@]}"

Sample output:


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). Now we can use bash for loop to read or print values from $distro:

## define it
distro=("redhat" "debian" "gentoo")
## get length of $distro array
## Use bash for loop 
for (( i=0; i<$len; i++ )); do echo "${distro[$i]}" ; done

Putting it all together

A sample shell script to print array called NAMESERVERS:

# define array
# name server names FQDN 
NAMESERVERS=("" "" "")
# get length of an array
# use for loop read all nameservers
for (( i=0; i<${tLen}; i++ ));
  echo ${NAMESERVERS[$i]}

Sample output:

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 17 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
17 comments… add one
  • S. Nilesh Jul 27, 2008 @ 16:47

    Nice one ;)

  • Diya Jul 28, 2008 @ 8:20

    Excellent info.

  • those Aug 5, 2008 @ 20:04

    how to clean the array? ;d

    • Dan Jun 16, 2010 @ 19:39

      Here’s a quick flush:

      NUM=${#a[@]}; for ((i=0;i<${NUM};i++)); do a[${i}]=''; done

      And a for a quick scan, you can do the following either before or after:

      COUNT=0; while [ ${COUNT} -lt ${#a[@]} ]; do echo ${a[${COUNT}]}; COUNT=$((COUNT+1)); done
  • budacsik Sep 20, 2008 @ 12:18

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


    for i in $NAMESERVERS; do
    echo “$i”

    • Dan Jun 16, 2010 @ 17:56

      That only works if you have no spaces.

      • Ferk Jan 3, 2012 @ 20:48

        The way it’s done in the post only works when using bash. This is a more POSIX-compatible way of making it work. You can also change the element-separator character to something different than the space, using the variabe IFS.

  • Raymond Feb 7, 2009 @ 10:11


    echo ${#a[@]}

    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

    Best Regards,

    • Dan Jun 16, 2010 @ 19:29

      Well yes, ${#a[@]} pretty much spits out the the number of elements in the array, like the document here said… notice that it’s plainly used as tlen which is the length of the array?

      you could do it a million different ways…

      for ((i=0;i<${NUM};i++)); do echo ${a[${NUM}]}; done


      COUNT=0; while [ ${COUNT} -lt ${#a[@]} ]; do echo ${a[${COUNT}]}; COUNT=$((COUNT+1)); done

      We could go on and on, since there's more than one way to skin a cat.

  • Stuart Holme Oct 27, 2010 @ 15:35

    Good points on handling a possibly sparse array, Raymond, but the example is pretty useful if you load the array yourself. Also note that you don’t need to use a variable to store the array length:

    for (( i = 0 ; i < ${#my_array[@]} ; i++ )) do; echo "${my_array[i]}"; done

    Note the implementations of BASH I am using don't require "$i" as the index of the array, they work fine with just "i". This only seems to work with single character length index variable names though, like "i", not with, for example, "index". YMMV.


  • Edward Aug 5, 2011 @ 7:17

    1.Write a Linux shell Script to count the Number of user accounts both normal and special or privileged user user accounts on the system.
    2.Write an interactive Linux shell script to test whether a file is (a). Read Permission set. (b). Execution permission set (c). Is a non empty file (d). A regular file (e). Ia a directory.

  • Ryan Oct 3, 2011 @ 21:48

    lol @ all the bikeshedding syntax scholars here. Ok, your voice has been heard, move along idiots.

    • Dan Jan 3, 2012 @ 22:30

      It’s called sharing knowledge. If you don’t like it, move along.

  • Jeff Oct 25, 2012 @ 9:25

    Dan I know this is an older thread but I found this and your information was very helpful to me. Unfortunately there are not enough people in the world like you and more like Ryan.

    Thanks again!

  • Raina May 9, 2013 @ 21:20

    Hi, Sorry i know this is an old thread!

    I need a help!

    for i in `seq 1 ${#DEV[@]}` ; do
      echo "DEV- ${DEV[$i]} ${DEV_DS[$i]} " >> $REPORT1

    ${#DEV[@]} –> this represents the length of the array

    If I want to go to the second last element of the array , what can I write in place of ${#DEV[@]} ?

    • Brian Jun 5, 2013 @ 14:59

      If ${#DEV[@]} is the last element then just decrement it to get the second last element.

      • arol Mar 9, 2017 @ 4:50

        Old thread; yep i’ve used below;

        $(( ${#arry[@]}-2 ))

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum