This statements works fine in BASH v. 4.1.7(1), but not in BASH v. 4.2.20(1)
num=${number:$counter:1}
I am getting “Bad Substitution” as the error. Any insights on why this is happening?
The objective is to do something similar to ${string:position:length} for getting sub-string.
Any help will be much appreciated. Thanks.
The original code which had this line is added below (line 15)
#!/bin/bash
echo "Please enter a number: "
read number
counter=0
answer=0
end=$(( ${#number} - 1 ))
echo -n "The sum of all digits of "
echo -n $number
echo -n " is "
while [ $counter -lt ${#number} ] ; do
num=${number:$counter:1}
if [ $counter -lt $end ] ; then
echo -n $num
echo -n "+"
else
echo -n $num
echo -n "="
fi
answer=$(( $answer + $num ))
counter=`expr $counter + 1`
done
echo $answer
the expression is fine with 4.2.20 when i tested it. maybe you have something in your env?