How do I use a Bash variable (string) containing quotes in a command?

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

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

eval is another option:

$ x="'a b'"
$ printf '%s,%s' $x
'a,b'
$ eval printf '%s,%s' $x
a b

See also:

  • https://unix.stackexchange.com/questions/38440/why-does-bash-variable-expansion-retain-quotes
  • https://stackoverflow.com/questions/7454526/bash-variable-containing-multiple-args-with-quotes

Alternatively, you can create an alias for your command:

alias myrsync='rsync --archive --exclude="foo bar.txt"'
myrsync dir1 dir2