Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Failing, simplified example:

FLAGS='--archive --exclude="foo bar.txt"'
rsync $FLAGS dir1 dir2

I need to include the quotes as if the command was like this:

rsync --archive --exclude="foo bar.txt" dir1 dir2
share|improve this question

2 Answers 2

up vote 8 down vote accepted

Short answer: see BashFAQ #50 ("I'm trying to put a command in a variable, but the complex cases always fail!").

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; if it was not in quotes, spaces in it are parsed as argument breaks, but quotes and escape are not parsed. In either case, putting quotes in the variable's value does nothing useful.

Usually, the best way to do this sort of thing is using an array instead of a simple text variable:

FLAGS=(--archive --exclude="foo bar.txt")
rsync "${FLAGS[@]}" dir1 dir2
share|improve this answer
    
typo on FLAGS- ? –  Sirex Nov 24 '11 at 8:38
    
@Sirex: Thanks; fixed now. –  Gordon Davisson Nov 24 '11 at 16:26

I don't see the problem :

$ FLAGS='--archive --exclude="foo bar.txt"'
$ echo $FLAGS
--archive --exclude="foo bar.txt"

Maybe you need to quote again the value :

$ rsync "$FLAGS" dir1 dir2
share|improve this answer
    
echo isn't showing what you think it is. Try printargs() { printf "'%s' " "$@"; echo; }; printargs $FLAGS; printargs "$FLAGS" to see why neither of these options work. –  Gordon Davisson Nov 25 '11 at 2:25

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.