A bash script has the following line
rsync $OPTS $BACKDIR $USER@$DEST:$DESTDIR
but one of the options is rsh='ssh -p2222'
It complains :
rsync: -p2222': unknown option
rsync error: syntax or usage error (code 1) at main.c(1425) [client=3.0.7]
But when I change the script to :
echo rsync $OPTS $BACKDIR $USER@$DEST:$DESTDIR
and manually run the printed command it works.
Please advise
Short answer: see BashFAQ #50.
Long answer: Putting commands (or parts of commands) into variables and then getting them back out intact is complicated. When the shell expands a variable on the command line, if the variable was in double-quotes it's not parsed (see @Dennis Williamson's answer); if it was not in quotes, spaces in it are parsed as argument breaks, but quotes and escape are not parsed (which is why you got an error about a literal quote mark in one of the args to rsync). In either case, putting quotes in the variable's value does nothing useful.
There are, however, several ways to fix your script, depending on how complex OPTS is:
If --rsh is the only thing in OPTS, then double-quote it (as in Dennis Williamson's answer):
(Note the double-quotes around the other args as well: this is to prevent misparsing if they contain spaces, and is generally good scripting hygiene.)
If the contents of OPTS are constant (or at least, the --rsh part is), then don't try to put it in a variable, just use it directly:
If neither of those simple cases apply, use an array instead of a plain variable:
To preserve the quoting and whitespace in the variable, you need to quote the variable when it's referenced:
I guess your
rsh
option should be used with rsync's-e
(resp.--rsh
) flag so that rsync knows to use the custom port for the ssh connection. Does your command look like so:It may be that the quotation marks around the 'ssh -p222' part are simply missing when rsync executes. You could try defining the
rsh
variable like this instead:so that it evaluates to "ssh -p222" during execution (literally, including the double quotes).
One option is to remove "space" from $IFS in your script:
Bash will then behave as most programmers would expect, since the spaces will no longer be used as field separators within the variables.