You need to move the position of the $ and add a last /
to the sub to perform it for all spaces. You also need to quote the variables as it will see the space in the filename as a delimiter between the arguments for mv.
#!/bin/bash
newFile=""
for file in *
do
newFile=${file// /_} # so as to replace spaces with underscores
mv "{$file}" "{$newFile}"
done
Although as stated in the other answer, half of this script is redundant.
newFile=""
Is not needed at all as the variable can be first referenced in the assignment later on.
newFile=${file// /_}
Although the assignment itself is also not needed as the parameter substituion does not alter the original.
So it can be rewritten as
#!/bin/bash
for file in *
do
mv "${file}" "${file// /_}"
done
sed -i s/ /_/g *
– amisax Jun 25 '15 at 13:50$
in front of the{
like${file}
and${file/ /_}
– Lambert Jun 25 '15 at 13:50${file// /_}
(a double slash after file) to have all the spaces replaced with an underscore instead of only the first one. – Lambert Jun 25 '15 at 13:54