I want to pass an argument to rsync
from a variable like this:
myopts='-e "ssh -p 1234" -a'
rsync $myopts 192.168.0.1:/a /a
For some reason this does not work. I even found this webpage where it says that it won't work:
### NO NO NO: this passes three strings:
### (1) "my
### (2) multiword
### (3) argument"
MYARG=""my multiword argument""
somecommand $MYARG
### THIS IS NOT (!!!!) THE SAME AS ###
command "my multiword argument"
### YOU NEED ###
MYARG="my multiword argument"
command "$MYARG"
Unfortunately it doesn't say why it won't work.
The script containing the rsync
call is used by a lot of other scripts, so I can only change it in a compatible way. A solutions that doesn't work is using an array:
myopts=('-a' '-v' '-z') # new way, would work
myopts='-a -v -z' # old way, breaks
rsync "${myopts[@]}" 192.168.0.1:/a /a
The options from the variable are completely ignored.
See Question&Answers more detail:os