Trouble in script with spaces in filename

You should quote both the declaration and the usage

path="/var/root/Documents/MyFile OG-v1.2.3.pkg"
scp "$path" [email protected]:/Users/Me/Desktop

If you do not quote the first, $path will contain just the first part. If you do not quote the second, scp will treat each space-separated part as an argument.

(I've changed $PATH to $path because $PATH is an important reserved variable and you must not use it for general purposes.)


I was trying something very similar with ssh and passing a command line through it. e.g.

ssh <somehost> ls -l "$PATH"

I found that simply defining "$PATH" didn't do the trick - it still threw up errors. However if I ran

ssh <somehost> ls -l "\"$PATH"\"

This worked. The trick is to ensure an additional set of " " gets passed to the ssh command from the shell.