I’m writing a Shell Script and an unexpected error (that I never saw before) appears.
The code is something like this:
num=1
case $line in
*.mp3*)
s$num=$total_seconds; # $total_seconds is a variable with numbers like: 11.11
a$num=$file;
num=$(($num + 1));
;;
esac
If I try to see the contents of these variables it doesn’t show anything.
echo $s1 $a1
echo $s2 $a2
or even:
for ((i=1; i<=$num; i++))
do
echo $s$i $a$i
done
bashonly recognizes a variable assignment if the LHS is a literal, not an expression. While you could modify your code slightly to have such dynamically generated variable names:a better idea is to use an array as suggested by kojiro.
To access them dynamically, you’ll need to use indirect expansion: