A "clean" way to parse a variable containing whitespaces and quotes to cp or mv

Hello,

I was wondering if there is any “clean” way to parse a variable containing whitespaces and quotes to cp or mv in bash.
Let’s say, we have a directory “test test” and want to move it into a directory “test2” from the same folder. But the “test test” is saved in a variable $fname.
If I now try something like


#!/bin/bash
fname="test test"
mv \'$fname\' test2/

or


#!/bin/bash
fname="'test test'"
mv "$fname" test2/

(echo $fname will give ‘test test’ here as output)
I get an error message such as

cp: cannot stat ‘'test test.D'’: No such file or directory

However, if I echo these commands instead of running them and then copy the results, paste them direct into terminal and run, they work.

#!/bin/bash
fname='test test'
fname2="'test test'"
echo $fname
echo $fname2
echo "cp -R '$fname' test2"
echo "cp -R $fname2 test2"

Both echos result in


cp -R 'test test' test2

Running which leads to the desired result.
My “dirty” solution to the whole problem is then


#!/bin/bash
echo "#!/bin/bash">runtest2.sh
chmod 755 runtest2.sh
fname='test test.D'
echo "cp -R '$fname' test2">>runtest2.sh
#echo the command into a new script
#there it will appear as "cp -R 'test test' test2"
./runtest2.sh

Which works (of course, the real problem does not contain one file with a known name but multiple file names stored in a file); but I wonder whether there is a “cleaner” solution for this not including creating and executing of a second script.
Does anyone have an idea?


touch 'test test'
var='test test'
ll 'test test'
mv "${var}" something-else

Hope that helps. Basically, use double-quotes so that you can get your
variable interpolation without string-concatenating single-quotes to the
whole thing which is messing you up. You may not need the braces around
the variable name, but it’s good practice regardless.


Good luck.

If you find this post helpful and are logged into the web interface,
show your appreciation and click on the star below…

That works indeed. Thanks, good to learn that. I have basically tried to use to many quotes …:slight_smile:

now that you have something ‘working’ let me pls add a more general answer.

A good read is http://mywiki.wooledge.org/BashPitfalls.
And also very important may be the size of the list you have to work on, so pls read about xargs.

I always use a while loop and read to get file names. If you use any search engine, you’ll find a lot about while loops in bash dealing with file names. Here is 1 example about globbing, special characters (like 2 dashes at beginning of file names) and read command in a while loop to process a list of file names:
http://unix.stackexchange.com/questions/9496/looping-through-files-with-spaces-in-the-names

HTH
Shvenky, posting from Shvenk’s Lair

Thank you for additional info. I was already using while and read in the full version of my “dirty” solution at the moment I posted the thread, but I was just curious why I could not use cp or mv directly.

Konstl