How to check if a array is empty in Bash
In this tutorial, we are going to learn about how to check if a array is empty or not in Bash.
Checking array is empty
To check if a array is empty or not, we can use the arithmetic expansion syntax (( )) in Bash.
Here is an example:
arr=()
if ((${#arr[@]})); then
echo "array is not empty"
else
echo "array is empty"
fiOutput:
"array is empty"or we can also check it like this:
arr=()
if [ ${#arr[@]} -gt 0 ]; then
echo "array is not empty"
else
echo "array is empty"
fiIn the example above, this syntax ${#arr[@]} returns the total number of elements in an array.


