Q:

Bash: adding array element different behavior from script vs interactive shell

I have the following code:
numbers=()
a=()
a+=('foo bar baz')
for ((x=0; x<=9; x++));
do
    numbers+=($x)
done

echo $numbers
echo $a

When I run this interactively in the shell via copy/pasting it, then I get the output that I want, which is: 
➜ echo $numbers
0 1 2 3 4 5 6 7 8 9
➜ echo $a
foo bar baz

However, when I run it as a script I get:
0
foo bar baz

Why? And how can I turn it into the behavior with the interactive shell?
Full disclosure: I Googled extensively and couldn't find it. I run zsh and assume it is the same as bash in this case. I'm not very familiar with bash and the interactive shell vs file differences.
Edit
After the helpful comments I now found the strange culprit.
I think it has something to do with chmod +x.
I created a simpler script:
for ((x=0; x<=9; x+=1)) do numbers+=($x); done; echo $numbers;
echo $SHELL

This is the output (I'm in a folder called magic):
chmod +x ./test
➜  magic ./test 
0
/bin/zsh
➜  magic zsh ./test 
0 1 2 3 4 5 6 7 8 9
/bin/zsh

Could it be that chmod +x sets it as a bash executable?

A:

You are running your code in zsh in your terminal, where echo $numbers outputs the contents of the entire array. In the script, it is run with sh (lacking a shebang), so echo $numbers is equivalent to echo ${numbers[0]}, outputing only the first element. (Your sh appears to be a link to bash, since the array syntax still works otherwise.)
Your a array, however, only has one element, the string foo bar baz, because you quoted it. To add multiple elements, you would use a+=(foo bar baz), so that the whitespace would be interpreted as separating separate array elements, rather than literal spaces.
The value of $SHELL is the name of your default shell, not the name of the shell that is currently running.

