I am trying to declare an array in bash, but when the code is run it says it cannot find the array. I have tried to write out the declaration of the array in several different ways, but it seems no matter how I try to declare it I cannot get it to work. I originally tried to declare it as such:
candidate[1]= 0
candidate[2]= 0
candidate[3]= 0
The error messages that are returned are:
votecalculation.sh: 13: candidate[1]=: not found
votecalculation.sh: 14: candidate[2]=: not found
votecalculation.sh: 15: candidate[3]=: not found
After this I tried another solution I found online:
ARRAY=( 'can1' 'can2' 'can3' )
When that is used it returns this error:
votecalculation.sh: 12: Syntax error: "(" unexpected
I am new to Bash and am getting really confused about arrays. Is there some specific way I need to declare an array or am I just going about it completely wrong?
It probably doesn't like the space after the equals sign.
Some other ideas:
Be sure that you're actually using bash to run your script, and not sh/dash.
You can explicitly declare a variable to be an array using declare -a varname
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With