4

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???

8

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.

  • Thank you for getting back with this. And, of course, it works. I didn't know about running a script with "bash" instead of "sh". – Bob-El Jun 8 '15 at 20:48
  • @user314397 it's all good. Sometimes at my job we say that the script always works when the other tech looks at it with you. :) Glad you got it working! – Terrance Jun 8 '15 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.