Search code examples
bashshellrsync

rsync not working with variable containing a white space


I cant get my rsync script to work if source or destination variable have a space in the folder name

#!/bin/bash
HOST="[email protected]"
source="/path/My Files/"
dest="/path/My Files2/"

rsync -avhP --delete "$HOST":"$source" "$dest"

the error i get is

receiving incremental file list
rsync: [sender] link_stat "/path/My" failed: No such file or directory (2)
rsync: [sender] change_dir "/root/Files" failed: No such file or directory (2)

Any help how to write this would be great


Solution

  • Add option -s with current rsync version.

    From man rsync:

    -s, --protect-args: This option sends all filenames and most options to the remote rsync without allowing the remote shell to interpret them. This means that spaces are not split in names, and any non-wildcard special characters are not translated (such as ~, $, ;, &, etc.). Wildcards are expanded on the remote host by rsync (instead of the shell doing it).