| by Arround The Web | No comments

How to Find the Array Length in Bash

Bash supports both numeric and associative arrays. The total number of elements of these types of arrays can be calculated in multiple ways in Bash. The length of the array can be counted using the “#” symbol or loop, or using a command like “wc” or “grep”.  The different ways of counting the array length in Bash are shown in this tutorial.

Find the Array Length Using “#”

Using the “#” symbol is the simplest way to calculate the array length. The methods of counting the total number of elements of the numeric and associative array is shown in this part of the tutorial.

Example 1: Count the Length of a Numeric Array Using “#”

Create a Bash file with the following script that counts and prints the length of a numeric array using the “#” symbol. The “@” and “*” symbols are used here to denote all elements of the array.

#Declare a numeric array

items=("Shirt" "T-Shirt" "Pant" "Panjabi" "Shoe")

#Count array length using '#'

echo "Array length using '#' with '@':  ${#items[@]}"

echo "Array length using '#' with '*':  ${#items[*]}"

The following output appears after executing the script. The array contains five string values and the same output is shown for both “@” and “*” symbols:

Example 2: Count the Length of an Associative Array Using “#”

Create a Bash file with the following script that counts and prints the length of an associative array using the “#” symbol. The “@” and “*” symbols are used here to denote all elements of the array.

#Declare an associative array

declare -A items=([6745]="Shirt (M)" [2345]="Shirt (L)" [4566]="Pant (36)")

#Count array length using '#'

echo "Associative array length using '#' with '@':  ${#items[@]}"

echo "Associative array length using '#' with '*':  ${#items[*]}"

The following output appears after executing the script. The array contains three string values and the same output is shown for both the “@” and “*” symbols:

Find the Array Length Using a Loop

Using a loop is another way to count the total number of elements in the array. The length of an array is counted using a while loop in the following example:

Example: Count the Length of an Array Using a Loop

Create a Bash file with the following script that counts the total number of elements using a “while” loop. A numeric array of four string values is declared in the script using the “declare” command. The “for” loop is used to iterate and print the values of the array. Here, the $counter variable is used to count the length of the array that is incremented in each iteration of the loop.

#Declare an array

declare -a items=("Shirt(M)" "Shirt(L)" "Panjabi(42)" "Pant(38)")

echo "Array values are:"

#Count array length using loop

counter=0

#Iterate the array values

for val in ${items[@]}

do

  #Print the array value

     echo $val

     ((counter++))

  done

  echo "The array length using loop is $counter."

The following output appears after executing the script. The array values and the length of the array are printed in the output:

Find the Array Length Using the “Wc” Command

The length of the array can be counted using some commands. The “wc” command is one of them. But this command does not return the correct output if the array contains the string value of multiple words. The method of counting the total number of elements of an array and comparing the array length value that is counted by the “#” symbol and “wc” command is shown in the following example.

Example: Count the Length of an Array Using the “Wc” Command

Create a Bash file with the following script that counts the total number of elements using the “wc” command. A numeric array of five string values is declared in the script. The “wc” command with the -w option is used to count the length of two arrays of 5 elements. One array contains a string of one word and another array contains a string of two words. The length of the second arrays is counted using the “#” symbol and the “wc” command.

#Declare a numeric array of a single word of the string

items=("Shirt" "T-Shirt" "Pant" "Panjabi" "Shoe")

echo "Array values: ${items[@]}"

#Count array length using 'wc'

len=`echo ${items[@]} | wc -w`

echo "Array length using 'wc' command: $len"

 

#Declare a numeric array of multiple words of the string

items2=("Shirt (XL)" "T-Shirt (L)" "Pant (34)" "Panjabi (38)" "Shoe (9)")

echo "Array values: ${items2[@]}"

echo "Array length using '#': ${#items2[@]}"

#Count array length using 'wc'

len=`echo ${items2[@]} | wc -w`

echo "Array length using 'wc' command: $len"

The following output appears after executing the script. According to the output, the “wc” command generates the wrong output for the array that contains a string value of two words:

Conclusion

The methods of counting the length of an array using the “#” symbol, loop, and the “wc” command are shown in this tutorial.

Share Button

Source: linuxhint.com

Leave a Reply