Are you looking for a tutorial on how to find the array length in Bash? Then this guide is for you.
Arrays are used to store multiple values in a single variable. Since there is no limit on how many elements you can store in an array, it’s very easy to forget the number of elements present in a particular array.
But the good thing is you can find the length of an array pretty easily. The length of an array indicates how many elements it contains. You can find the length in various methods like using the # symbol, using loops, printing the indices, etc.
We will go through all the possible methods of finding the array length in Bash. Let’s get started!
How to Find the Array Length in Bash
Since there are numerous methods to do this, we’ll go through each so you can decide which one you’re more comfortable with. First, let’s get our setup ready.
Create a Bash script file using this command:
$ touch arrlength.sh
Then make the file executable with this command:
$ chmod u+x arrlength.sh
Output:

Now open the Bash script in any text editor you like. We will use nano for this tutorial. To open the script in nano, run this command:
$ nano arrlength.sh
Now you’re ready to start writing some Bash commands. Follow along as we see how you can find array lengths in Bash.
Get Array Length Using the # Symbol
This is by far the simplest way to do this. Using the #
symbol, you can get the array length with only one line of code. Let us show you how. Look at the below example:
#! /bin/bash
distro=('ubuntu' 'linuxmint' 'debian' 'kali')
echo "The length of the array is: ${#distro[@]}"

Once you’ve written the script, save it with ‘Ctrl + O’ and exit from nano using ‘Ctrl + X’. Now run the script with this command:
$ ./arrlength.sh
Output:

As you can see from the above screenshot, the array length is 4, which is correct.
In Bash, ${#array[@]}
is a special syntax that allows you to get the length of an array. ${#array}
gives you the length of the first element of the array(which is 6 in this case). Adding [@]
to the syntax gets you the length of the entire array instead of just the first element. [@]
is used to specify all elements in the array.
Get Array Length by Retrieving Array Indices
Another way to get the length of Bash arrays is to get the index numbers of all elements present in the array. We can retrieve the index numbers using the !
sign.
However, you should know that in Bash, the index of arrays starts from 0. That means if you add 1 to the last elements index, you can get the array’s length. It will become clearer to you once you see this example:
#! /bin/bash
distro=('ubuntu' 'linuxmint' 'debian' 'kali')
echo "The indices of the elements of the array: ${!distro[@]}"

Save the script and exit. Then run the script with this command:
$ ./arrlength.sh
Output:

The index of the last element is 3. If you add 1 to it, it becomes 4, which is indeed the length of the array. Since indexing starts from 0, we increase the last index by one to get the length of the array.
Get the Array Length Using a Loop
If you’re an experienced programmer, you may be familiar with iterating through arrays. If that’s what you prefer to do to flex your programming muscles, then this method is for you.
We use a loop to iterate through each element of the array. While traversing through the array, we keep count of the total number of elements in that array. We save that in a variable and print it out later. Let’s look at an example:
#! /bin/bash
distro=('ubuntu' 'linuxmint' 'debian' 'kali')
# Initialize a counter
length=0
# Iterate over the array elements
for element in "${distro[@]}"; do
((length++))
done
# Print the length
echo "The length of the array is: $length"

Save the exit. Then run the script with this command:
$ ./arrlength.sh
Output:

Use take a variable and initialize it as 0. When going through the array in the loop, we increase the value of that variable by one each time. When we’re done traversing, the variable’s value will be the same as the number of elements in the array. We then simply print the variable.
This method is useful if you forget the special syntax of the previous methods. You can use your loop knowledge to get the array size.
Get the Array Length Using the “wc” Command
The wc
Bash command counts the number of lines, words, characters, and bytes in files. If we convert the array into a string, we can count the number of words in that string and determine the size of the array. Have a look at this example:
#! /bin/bash
distro=('ubuntu' 'linuxmint' 'debian' 'kali')
# Convert array to string
array_string="${distro[@]}"
# Count the number of words in the string
length=$(echo "$array_string" | wc -w)
# Print the length
echo "The length of the array is: $length"

Save the script, exit from the editor, and then run it with this command:
$ ./arrlength.sh
Output:

It works as expected. So what is happening is that we are converting our array into a string. We count the words of that string using wc -w
. The -w
flag is used for counting words. We then store the word count in a variable and print that. You can learn more about this command on its manual page.
One downside to this method is that all the elements in the array must not contain any space character. In that case, you will get the wrong length. Let us demonstrate it:
#! /bin/bash
distro=('ubuntu' 'linuxmint' 'debian' 'kali' 'pop os')
# Convert array to string
array_string="${distro[@]}"
# Count the number of words in the string
length=$(echo "$array_string" | wc -w)
# Print the length
echo "The length of the array is: $length"

Upon running the script, this is what we see:

The length should’ve been 5 since we added a single element. However, due to the space in ‘pop os’, they are counted as two separate words in the string. So we get an additional word when counting the length
Final Thoughts
This guide shows you how to find the array length in Bash. We’ve covered many possible methods of doing it. Feel free to use any that you find best. If you have any questions about any of the methods, feel free to let us know in the comments down below.
If this guide helped you, please share it.