1

Trying to get a script to properly make use of variables. (The below examples are on the command line but I see the exact same behavior when inside a #!/bin/bash script.

$ FLAGS='--rsh="ssh -2"'
$ rsync $FLAGS foo bar
rsync: -2": unknown option
rsync error: syntax or usage error (code 1) at main.c(1084)

So then I add quotes.

$ rsync "$FLAGS" foo bar

And now it works properly. Okay. Now let's add some more flags to the $FLAGS variable. (I tried it with just "-r" and just "-p", the same thing happens, so I don't think it's related to the particular single-hyphen flags I'm passing.)

$ FLAGS='-rptvlCR --rsh="ssh -2"'
$ rsync $FLAGS foo bar
rsync: -2": unknown option
rsync error: syntax or usage error (code 1) at main.c(1084)
$ rsync "$FLAGS" foo bar
rsync: -rptvlCR --rsh="ssh -2": unknown option
rsync error: syntax or usage error (code 1) at main.c(1084)
$ 

Notice in the second case it's treating the entire argument as a single option to rsync.

The basic command (typed out by hand without using the $FLAGS variable) works properly.

Any ideas?

Any ideas? I've read through all the bash scripting docs I can find, and I can't figure out why rsync is ignoring the double quotes some of the time and treating -2" as a single token.

1 Answer 1

1

Throw them into an array:

FLAGS=( '-rptvlCR' '--rsh="ssh -2"' )
rsync "${FLAGS[@]}"

See this Bash FAQ for more detail

1
  • Brilliant! Thanks. That FAQ is highly useful, too.
    – dirtside
    Jan 27, 2012 at 0:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .