Home > database >  How to expand variable whose value contains space properly in scp command?
How to expand variable whose value contains space properly in scp command?

Time:08-01

When a file name contains spaces ,the file name need escape two times in scp command.
I can get file "somesoft someversion.apk" from remote ip address into my local "tmp" directory:

scp user@ip_address:/share/"somesoft\\ someversion.apk"  /tmp

It works fine.Now i want to use a var name to refer to the file "somesoft someversion.apk".

fname="somesoft someversion.apk"
scp user@ip_address:/share/"$fname"  /tmp

The var can't be parsed properly in scp command:

scp: /share/somesoft: No such file or directory
scp: someversion.apk: No such file or directory

How to expand variable whose value contains space properly in scp command?
Now here is the real case,scp works fine with two time escapes:

enter image description here

Now to set a var and assign value with it,then scp: enter image description here

"'$fname'" have no magic effect in this case.

CodePudding user response:

You have to shell quote it.

fname="somesoft someversion.apk"
scp user@ip_address:/share/"$(printf "%q" "$fname")"  /tmp

CodePudding user response:

I recommend you to read my quoted post, it will help you to understand why. Try to add extra quotes:

    fname="somesoft someversion.apk"
    scp user@ip_address:"'/share/$fname'"  /tmp

Solution based on this original post

  • Related