Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading