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?

2
  • 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! Commented May 6, 2010 at 5: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. Commented May 6, 2010 at 15:30

8 Answers 8

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

Sign up to request clarification or add additional context in comments.

2 Comments

It says that declare is not found when I try to declare the array...it is just so odd that the rest of the program runs after pushing out the errors.
Are you sure you're running bash?
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

9 Comments

It did not find those either and for the echo it says it is a "bad substitution"
do you have some strange implementation of bash? How are you calling the script?
I don't think I do. I call the script from the terminal using "sh votecalculations.sh"
try ./votecalculations.sh or . ./votecalculations.sh
but try that first with the code i pasted, in case you have other errors in your code
|
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.

1 Comment

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.
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 !

Comments

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' )

1 Comment

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.
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).

Comments

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

Comments

-2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.