I was trying to ensure that a directory path in a Bash script always ended
in a slash, as follows:
if [ "${dir:-1:1}" != "/" ]; then
dir="${dir}/"
fi
but it wouldn't work. The substitution "${dir:-1:1}" was supposed to give me
the last character of the string in the variable "dir", but it was instead
returning the whole string. Checked the Bash manual
<http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion>:
${parameter

ffset:length}
... If offset evaluates to a number less than zero, the value is used as
an offset from the end of the value of parameter.
Yup, that's what I wanted. Then I realized: "-1" is probably not valid as a
numeric literal, I have to insert an _expression_ if I want it to evaluate
to a negative number. And so this
if [ "${dir:$((-1)):1}" != "/" ]; then
dir="${dir}/"
fi
worked!