It sounds like you're not using bash
at all. I can only reproduce the error you show if I use dash
instead of bash
:
bash
:
$ line="someline content"
$ echo ${line}
someline content
$ lastchars=${line: -5}
$ echo ${lastchars}
ntent
dash
:
$ line="someline content"
echo ${line}
lastchars=${line: -5}
echo ${lastchars}
$ someline content
$ dash: 3: Bad substitution
Your shebang line is pointing to bash
, but you are running the script with sh
, so the shebang is ignored. /bin/sh
on Ubuntu systems is actually dash
, a minimal shell that doesn't support the syntax you are trying to use.
When using a shebang line, there's no reason to explicitly call a shell for the script, just make it executable (chmod a+x /path/to/script.sh
) and run it without specifying an interpreter:
/path/to/script.sh
Alternatively, just use the right one:
bash /path/to/script.sh
echo
? If so, please show us the contents of the$line
variable. Do you also get the error if you use quotes (echo "${lastchars}"
)? – terdon Nov 20 '17 at 11:11${line}
content prints just fine. anyways the substitution error is shown. @terdon theecho ${lastchars}
is not reached at all, because getting the last 5 characters from the variable already produces the error. – membersound Nov 20 '17 at 11:22sh
which is a different shell:dash
. See my answer. – terdon Nov 20 '17 at 11:37