#!/bin/bash
jobname="job_201312161447_0003"
jobname_pre=${jobname:0:16}
jobname_post=${jobname:17}
This bash script gives me Bad substitution error on ubuntu. Any help will be highly appreciated.
It's a syntax error that occurs when you execute your Bash script and it can be caused by different reasons. Two common causes for this error are the incorrect use of command substitution and incorrect characters added to the lines of your script (for example extra dollar signs or white spaces).
${} Parameter Substitution/Expansion A parameter, in Bash, is an entity that is used to store values. A parameter can be referenced by a number, a name, or by a special symbol. When a parameter is referenced by a number, it is called a positional parameter.
bash is sh, but with more features and better syntax. Bash is “Bourne Again SHell”, and is an improvement of the sh (original Bourne shell). Shell scripting is scripting in any shell, whereas Bash scripting is scripting specifically for Bash. sh is a shell command-line interpreter of Unix/Unix-like operating systems.
The default shell (/bin/sh
) under Ubuntu points to dash
, not bash
.
me@pc:~$ readlink -f $(which sh)
/bin/dash
So if you chmod +x your_script_file.sh
and then run it with ./your_script_file.sh
, or if you run it with bash your_script_file.sh
, it should work fine.
Running it with sh your_script_file.sh
will not work because the hashbang line will be ignored and the script will be interpreted by dash
, which does not support that string substitution syntax.
I had the same problem. Make sure your script didnt have
#!/bin/sh
at the top of your script. Instead, you should add
#!/bin/bash
For others that arrive here, this exact message will also appear when using the env variable syntax for commands, for example ${which sh}
instead of the correct $(which sh)
Your script syntax is valid bash and good.
Possible causes for the failure:
Your bash
is not really bash but ksh
or some other shell which doesn't understand bash's parameter substitution. Because your script looks fine and works with bash.
Do ls -l /bin/bash
and check it's really bash and not sym-linked to some other shell.
If you do have bash on your system, then you may be executing your script the wrong way like: ksh script.sh
or sh script.sh
(and your default shell is not bash). Since you have proper shebang, if you have bash ./script.sh
or bash ./script.sh
should be fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With