8

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?

Waffle
  • 302
  • 1
  • 3
  • 12
  • Ok, so in the end I figured out the problem. Even though the head of the file was #!/bin/bash the file name ended with .sh. After changing the file extension to .bash and running it using "bash votecalculations.bash" in terminal it works. Thanks again to everyone for your help! – Waffle May 06 '10 at 05:00
  • 2
    FYI the file extension doesn't matter... you can leave it as `.sh` or even have no file extension at all, as long as you run it with bash. – David Z May 06 '10 at 15:30

8 Answers8

11

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

Chris AtLee
  • 7,798
  • 3
  • 28
  • 27
5
 #!/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

tripleee
  • 175,061
  • 34
  • 275
  • 318
Bryan
  • 5,065
  • 10
  • 51
  • 68
  • It did not find those either and for the echo it says it is a "bad substitution" – Waffle May 06 '10 at 04:31
  • do you have some strange implementation of bash? How are you calling the script? – Bryan May 06 '10 at 04:34
  • 1
    I don't think I do. I call the script from the terminal using "sh votecalculations.sh" – Waffle May 06 '10 at 04:35
  • try ./votecalculations.sh or . ./votecalculations.sh – Bryan May 06 '10 at 04:37
  • but try that first with the code i pasted, in case you have other errors in your code – Bryan May 06 '10 at 04:37
  • If you're calling it explicitly with sh, that overrides whatever #! you have in the script itself. On Ubuntu, I believe sh is actually dash, not bash. What if you run via "bash votecalculations.sh"? – Chris AtLee May 06 '10 at 04:38
  • First one: sh: Can't open ./votecalculations.sh Second one just jumps to a new line and waits for another command. – Waffle May 06 '10 at 04:39
  • as i said, try my script first. yours might have other errors. – Bryan May 06 '10 at 04:42
  • It works when it is in the situation as you posted. It must be something else wrong with my code. Guess it is time to dig a little deeper. Thanks for all your help. – Waffle May 06 '10 at 04:45
4

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.

David Z
  • 128,184
  • 27
  • 255
  • 279
  • votecalculation.sh: 13: candidate[1]=0: not found votecalculation.sh: 14: candidate[2]=0: not found votecalculation.sh: 15: candidate[3]=0: not found I have tried it without spaces and above is the error I get when I try no spaces. – Waffle May 06 '10 at 04:24
2

I had the same problem. I tried to call my script with ./my_script.s, with . ./my_script.sh and I also tried with "bash my_script.sh" and "sh my_script.sh". I always got the same message : "my_array[1]: command not found".

Then I saw Chris AtLee's comment about bash not liking the space after the equal sign.

The exact lines in my code before

my_array[1] = 34
my_array[2] = 57

I removed the spaces before and after the equal signs.

my_array[1]=34
my_array[2]=57

Then, I simply tried the following in the terminal and didn't have the error message anymore.

$ my_script.sh

NOTE: Bash does not like spaces in variable definition !

Hope this helps other beginners like me !

The Demz
  • 7,066
  • 5
  • 39
  • 43
1

In the first one there should be no spaces after the equal signs.

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

The second one looks correct. Are you sure your shell is bash? Try adding a proper hash-bang line to the top of your script if you don't already have it:

#!/bin/bash
ARRAY=( 'can1' 'can2' 'can3' )
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • I have that exact hash-bang and it still does not work. I have tried the code on my Mac as well as my Ubuntu machine. – Waffle May 06 '10 at 04:27
0

If you have the correct shebang and you chmod +x scriptname, you don't need to start the script using bash scriptname - you can just use ./scriptname or if the directory it's in is in your PATH, then you can start it using simply scriptname.

If you have #!/bin/bash as your shebang and run sh scriptname then the shebang is overridden by the choice of shell on the command line.

There's no special meaning to having .sh or .bash at the end of the filename. It's just a matter of style or preference that some people like since it's intended to indicate the type of script (but only to the user - not to the system).

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
0

Here's a sample how to define and iterate over a bash array

#!/bin/bash
test_array=(el1 el2 el3)
for value in "${test_array[@]}";
do
  echo "$value"
done

Output:

el1
el2
el3
tripleee
  • 175,061
  • 34
  • 275
  • 318
David Thomas
  • 4,027
  • 3
  • 28
  • 22
-2

May be, bad declaration of shebang... Be sure, use #!/bin/bash as shebang