Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have an array which gets filled with different error messages as my script runs.

I need a way to check if it is empty of not at the end of the script and take a specific action if it is.

I have already tried treating it like a normal VAR and using -z to check it, but that does not seem to work. Is there a way to check if an array is empty or not in Bash?


share|improve this question

1 Answer 1

Supposing your array is $errors, just check to see if the count of elements is zero.

if [ ${#errors[@]} -eq 0 ]; then
    echo "No errors, hooray"
    echo "Oops, something went wrong..."
share|improve this answer
Perfect, thank you Michael. –  Marcos Sander Feb 11 '13 at 4:19
Please note that = is a string operator. It happens to work fine in this case, but I'd use the proper arithmetic operator -eq instead (just in case I'd want to switch to -ge or -lt, etc.). –  musiphil Nov 8 '13 at 19:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.