Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash : Bad Substitution

#!/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.

like image 620
Arindam Choudhury Avatar asked Dec 16 '13 16:12

Arindam Choudhury


People also ask

What does Bash bad substitution mean?

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).

What is ${} in Bash?

${} 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.

What is the difference between Bash and sh?

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.


4 Answers

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.

like image 113
Vanni Totaro Avatar answered Oct 18 '22 21:10

Vanni Totaro


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
like image 27
Guest Avatar answered Oct 18 '22 21:10

Guest


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)

like image 81
Nacho Coloma Avatar answered Oct 18 '22 22:10

Nacho Coloma


Your script syntax is valid bash and good.

Possible causes for the failure:

  1. 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.

  2. 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.

like image 24
P.P Avatar answered Oct 18 '22 22:10

P.P