-2

The following piece of code where I want to check if an array is empty gives the error - re.sh: line 3: [0: command not found. The error is at the if statement. What is wrong with it?

#!/bin/bash
unique_hero=()
if [${#unique_hero[@]} -eq 0]
 then unique_hero+=('asdf')
 echo "${unique_hero[@]}"
fi

closed as off-topic by Jorge Castro, karel, Maythux, Danatela, Radu Rădeanu Apr 16 '14 at 7:08

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This is not about Ubuntu. Questions about other Linux distributions can be asked on Unix & Linux, those about Windows on Super User, those about Apple products on Ask Different and generic programming questions on Stack Overflow." – Jorge Castro, karel, Maythux, Danatela, Radu Rădeanu
If this question can be reworded to fit the rules in the help center, please edit the question.

2

Your script is correct, but remember to use the spaces before and after the brackets

#!/bin/bash
unique_hero=()
if [ ${#unique_hero[@]} -eq 0 ]
   then unique_hero+=('asdf')
echo "${unique_hero[@]}"
fi

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