Bash Check Empty Array

Written by: Linuxopsys   |   Last updated: April 10, 2023

Knowing whether the array is empty or can be significant especially when it comes to populating the elements with the data. An array can be considered empty if it doesn’t have any elements or it has been declared but is still uninitialized. 

Check if array is empty

"If statements", also referred to as conditional statements allow us to make decisions in our Bash scripts. We can check if an array is empty by finding its length and using it inside the condition. The condition in the if statement is evaluated based on the exit status. An exit status of 0 implies that it completed successfully. 

We can find whether an array is empty or not using the length of the array and use it with the Bash if-then-else statement. 

Using @

We can find the number of members in the array using the @ symbol as follows:

Example:

#!/bin/bash
arr=("Pushkin" "Tolstoy" "Chekhov" "Gorky" "Turgenev")	#non-empty array
if [ ${#arr[@]} -gt 0 ]
then
    echo Not empty
else 
    echo empty
fi

arr2=()			#empty array
if [ ${#arr2[@]} -gt 0 ]
then
    echo Not empty
else 
    echo empty
fi
Check if array is empty using @

The presence of @ expands all the elements of the array. Prefixing the # symbol to that array variable finds the number of members present. We have taken 2 arrays with one of them being declared as well as populated with values The other array is just declared. The if statement will check the size of the array in both the cases and decide whether to execute the if block or the else block. Based on that it will echo accordingly.

Using *

We can also find the array length using the @ symbol as follows:

Example:

#!/bin/bash
arr=("Pushkin" "Tolstoy" "Chekhov" "Gorky" "Turgenev")
if [ ${#arr[*]} -gt 0 ]
then
    echo Not empty
else 
    echo empty
fi

arr2=()			#empty array
if [ ${#arr2[*]} -gt 0 ]
then
    echo Not empty
else 
    echo empty
fi
Check if array is empty using *

The * symbol is a quantifier which means "match zero or more". When placed inside the array variable it pulls all the array members. Just like we did last time, prefixing the # symbol to that array variable will give us the array length. Using it with the if statement, helps decide whether the bash array is empty or not.

Using grep

The grep command is an extremely useful tool to filter searches. The keywords can be strings, particular patterns of characters, or regular expressions.

Example:

#!/bin/bash
arr=("Pushkin" "Tolstoy" "Chekhov" "Gorky" "Turgenev")
result=$(echo ${arr[@]} | grep -o " " | grep -c " ")
if [ $result -gt 0 ]
then
   echo Not empty
else 
    if [ ! -z "${arr[0]}" ]
        then
                echo Not empty
        else
                echo empty
        fi
fi


arr2=()			#empty array
result=$(echo ${arr2[@]} | grep -o " " | grep -c " ")
if [ $result -gt 0 ]
then
   echo Not empty
else 
    if [ ! -z "${arr2[0]}" ]
        then
                echo Not empty
        else
                echo empty
        fi
fi
Check if array is empty with grep

We know that Bash splits the array into a series of words which are separated by spaces. Hence, we are determining the number of blank spaces with the help of the grep command, using a blank space as the pattern for grep. The grep command used with -o option only pulls the matching pattern, which is a space. The -c option only prints the count of the lines that match the pattern. 

Using wc command

The wc command is an equally helpful tool to get count of words, characters or lines.

Example:

#!/bin/bash
arr=("Pushkin" "Tolstoy" "Chekhov" "Gorky" "Turgenev")
result=`echo ${arr[@]} | wc -w`
if [ $result -gt 0 ]
then
   echo Not empty
else 
   echo empty
fi
Check if array is empty with wc

We have taken the same approach which we did with grep to find the array size, which is to count the number of words in the array. The result of the echo command is sent as input to the wc command using the pipe operator. This command when used with the -w option counts the number of words available in the provided input. If the result is greater than zero, the array is not empty.

Conclusion

  • Prefixing the # symbol to that array variable returns the array size.
  • An array is empty if it doesn’t contain any element.
  • There are different approaches to check if the array is empty.
SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment