Ubuntu – I keep getting bad substitution error on bash scripts with arrays

bashscripts

I've been working on a Bash script using an array. I keep getting this message, "Bad substitution". So I Googled the problem and every example I came up with supposedly works but not on my computer. Here's a simple script that supposedly works.

#!/bin/bash
o="12345"
a=o
b=${!a}
echo ${a}
echo ${b}

But when I run it, I get this…

bob@Bobz-DT:~/IStests/test script$ sh test4.sh
test4.sh: 4: test4.sh: Bad substitution

I'm running Ubuntu 14.04 and the Bash version is…
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)

Can someone please tell me what's going on???

Best Answer

You need to run the script as bash and not sh.

So, run it with

bash test.sh

The other way, you could set the execute bit on your file by doing

chmod +x test.sh

then just run your script as

./test.sh

because you already have your command shell interpreter (shebang) as your first line #!/bin/bash.

Related Question