I'm trying to copy multiple files to a directory from within a shellscript. These files contain all sorts of "ugly" characters, such as whitespaces, brackets and what not else. However, I'm stuck when it comes to escaping these as bash and cp seem to handle these very strangely.
Here is the scenario:
When issuing this command from within my shell it works like a charm:
cp /somedir/a\ file.png /somedir/another\ file.png /someotherdir/
However, when reading the files to copy from a string, it suddenly becomes weird:
var="/somedir/a\ file.png /somedir/another\ file.png"
cp "$var" /someotherdir/
Results in cp: cannot stat 'a\\ file.png another\\ file.png': No such file or directory
I believe this is due to the fact that I give the variable as a string and cp believes it's one file, although it's multiple files. When issuing the same command without putting the variable in quotes (var="/somedir/a\ file.png /somedir/another\ file.png"; cp $var /someotherdir/) I get an even more weird error:
cp: cannot stat 'a\\ file.png another\\ file.png': No such file or directory
cp: cannot stat 'file.png': No such file or directory
cp: cannot stat 'another\\': No such file or directory
cp: cannot stat 'file.png': No such file or directory
It seems to completely ignore my escaping. What am I doing wrong?
EDIT:// It seems like Copy list of files has an answer with xargs, but I still wonder why bash is acting so weird here.