Patrick Patrick - 10 months ago 20
Bash Question

Error in creating array variable in Debian 8.4

I have a very basic shell script containing

echo ${NAME[0]}

So when I run this script an error occurs stating

./ 2: ./ NAME[0]=Hello: not found
./ 3: ./ Bad substitution

So basically I looked through a few tutorials and found this to be the basic way to declare arrays. So I am confused as to why this is an error. Any ideas?


You are starting your script as #!/bin/sh, which has a soft link to dash (The current version of sh which conforms with the POSIX 1003.2 and 1003.2a specifications for the shell) and dash doesn't support arrays. In debian 8 onwards dash has become default shell, so if you run ls -la /bin/sh the output will be /bin/sh -> dash

However bash still remains the default login shell, only the default /bin/sh used in shell scripts has been changed. So if you run your code on terminal, it will work just fine. More information on why this switch was made in Ubuntu can be found here.

If you want to use arrays in your script then you must start your script with #!/bin/bash

So your script works perfectly if modified like this

echo ${NAME[0]}

More information on Dash as Sh DashAsBinSh