Why do you need to escape spaces when passing SCP filenames

scp

Anyone who's ever SCP'd knows that this probably doesn't work:

scp foo.txt 'me@remote.net:foo bar.txt'

For me on this one, I get: "ambiguous target".

The man page for SCP doesn't explain why you need to escape that space, yet the argument seems to fit the specification for that argument listed in the man page:

[[user@]host1:]file1 

Seems to match, to me. So, why is it required to do this:

scp foo.txt 'me@remote.net:foo\ bar.txt'

Best Answer

In the first case, you are specifying two files as copy target, both residing on remote.net. This is clearly ambiguous, as the program cannot determine where to copy the file.

Consider the following, working example specifying two source files and one destination (directory) using the same format:

scp 'me@example.org:file1 file2' .

This will copy both file1 and file2 from example.org to the local directory.


Your local scp program connects to the remote host and starts another instance of of scp with a parameter determining whether you're transferring files from it (-f), or to it (-t). You can see this when you add the argument -v for verbose output. The source/destination parameter you enter (host:file) is split and the file names part simply appended as additional parameters specifying the files to use.

When using it as source:

debug1: Sending command: scp -v -f -- file1 file2

This works without problem, it transfers multiple source files over the network.

When using it as sink:

debug1: Sending command: scp -v -t -- file1 file2

This fails, as you're intending to write to multiple files. Now the way around this is to escape the file name again (once for the local shell, once for the remote call):

$ scp foo 'me@example.org:foo\ bar'
[...]
debug1: Sending command: scp -v -t -- foo\\ bar

And FWIW, scp foo 'me@example.org:"foo bar"' works just as well.

Related Question