Using: rsync version 3.1.0 protocol version 31; Linux Mint 17 (based on: Ubuntu 14.04.3)
In one of my backup Bash scripts, which uses rsync, I put the rsync options in a variable like this:
# Set rsync command options.
rsync_options="-e ssh -axhPv"
if [ "$deletion_type" = "DELETE_ON_DESTINATION" ]; then
rsync_options="$rsync_options --delete"
fi
if [ "$run_type" = "DRY_RUN" ]; then
rsync_options="$rsync_options --dry-run"
fi
# Run the backup.
rsync "$rsync_options" --log-file="$log_file" --exclude-from="$exclude_file" \
"$source_dir" "$destination_dir"
And got errors:
unknown option -- h
usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
[-D [bind_address:]port] [-E log_file] [-e escape_char]
[-F configfile] [-I pkcs11] [-i identity_file]
[-L [bind_address:]port:host:hostport] [-l login_name] [-m mac_spec]
[-O ctl_cmd] [-o option] [-p port]
[-Q cipher | cipher-auth | mac | kex | key]
[-R [bind_address:]port:host:hostport] [-S ctl_path] [-W host:port]
[-w local_tun[:remote_tun]] [user@]hostname [command]
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(226) [sender=3.1.0]
Instinctively I removed the quotes around "$rsync_options"
in the Bash script and tried again...
rsync $rsync_options --log-file="$log_file" --exclude-from="$exclude_file" \
"$source_dir" "$destination_dir"
...that fixed it, and it worked perfectly.
Placing the options of a command in variables in Bash scripts is something that I have done many times but I do remember that from time-to-time I've had to remove double quotes around a variable name, though I can't think of any specific occasion (apart from this one obviously).
My question is why did the quotes cause problems, and were they caused by something specific to rsync or to Bash? I'd like to get an understanding of this issue.
Thanks.