Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 15 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

You must log in to answer this question.

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