When I print a variable with one substring replacing another as described in this documentation, it behaves correctly:
stringZ='abc - 123'
echo $stringZ # abc - 123
echo ${stringZ/ - /test} # abctest123
However, if I get the string from a filename, the replacement is ignored completely:
for f in mp3/*; do
stringZ=$(basename $f)
echo $stringZ # abc - 123.txt
echo ${stringZ/ - /test} # abc - 123.txt
done
How can I replace a substring in a variable derived from a filename?