Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array in Bash Not Found [duplicate]

Tags:

linux

bash

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?

like image 283
Waffle Avatar asked May 06 '10 04:05

Waffle


1 Answers

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

like image 55
Chris AtLee Avatar answered Oct 29 '22 08:10

Chris AtLee