bash: Bad Substitution

Arindam Choudhury picture Arindam Choudhury · Dec 16, 2013 · Viewed 266k times · Source
#!/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.

Answer

Vanni Totaro picture Vanni Totaro · Dec 16, 2013

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.