Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Throw them into an array:

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

See this Bash FAQ for more detail

share|improve this answer
    
Brilliant! Thanks. That FAQ is highly useful, too. –  dirtside Jan 27 '12 at 0:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.