如何在scp中逃脱

Sor*_*ome 4 shell scp escaping

是的,我确实已经意识到已经被问过一千次如何在scp中转义空格,但是我没有用& -sign 来做,所以如果那个符号是目录名的一部分.

[sorunome@sorunome-desktop tmp]$ scp test.txt "bpi:/home/sorunome/test & stuff"
zsh:1: command not found: stuff
lost connection
Run Code Online (Sandbox Code Playgroud)

&符号似乎搞乱了很多东西,使用\&将无法解决问题,因为找不到远程目录:

[sorunome@sorunome-desktop tmp]$ scp test.txt "bpi:/home/sorunome/test \& stuff"
scp: ambiguous target
Run Code Online (Sandbox Code Playgroud)

甚至没有通过省略引号并在整个地方添加\这是有效的:

[sorunome@sorunome-desktop tmp]$ scp test.txt bpi:/home/sorunome/test\ \&\ stuff
zsh:1: command not found: stuff
lost connection
Run Code Online (Sandbox Code Playgroud)

那么,任何想法?

Aar*_*ron 7

逃离空间和&符号对我来说是个窍门:

scp source_file "user@host:/dir\ with\ spaces\ \&\ ampersand"
Run Code Online (Sandbox Code Playgroud)

出于某种原因,仍然需要报价.

  • 需要引号来保护反斜杠本身.`scp`有点棘手,因为参数被处理两次:在运行`scp`之前由本地shell处理一次,在文件名发送到远程主机之后再次*.(可能有助于将`scp`想象成像'cat source_file | ssh user @ host'cat>"/ dir with spaces&ampersand"''这样的包装器. (4认同)