Array in Bash Not Found

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


 #!/bin/bash

 myarray[0]=hello
 myarray[1]=world

 echo "${myarray[0]}"
 echo "${myarray[1]}"

save that to helloworld.bash and chmod +x the file.

execute using ./helloword.bash


Try removing the space:

candidate[1]=0
candidate[2]=0

and so on. I'm not an expert in this area myself but I think bash needs to recognize the whole assignment expression as one word, so you can't have spaces in it.

Tags:

Linux

Bash