Printing arrays are not working in shell script

I am kind of new in shell scripting and trying to learn arrays. I declared array value but when I am trying to print that array it is giving me an error(bad substitution).
I am pasting the code below, please suggest to me what is wrong here-

➜  ~ cat test.sh
#!/bin/bash

array=['foo','bar','a','b']
echo 1
echo "${array[0]}"
➜  ~ sh test.sh
1
test.sh: 5: Bad substitution

Thanks in advance.

>Solution :

Depending on the system you’re using sh might be not be Bash and
it’s not Bash on yours, it can be dash for example. Run your script with Bash:

$ bash arr.sh
1
[foo,bar,a,b]

Or set an executable bit and call the script without providing the name of the interpreter since you already have the shebang:

$ chmod +x test.sh
$ ./test.sh
1
[foo,bar,a,b]

Leave a Reply