76

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

6 Answers6

86

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
8

eval is another option:

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

See also:

4

Alternatively, you can create an alias for your command:

alias myrsync='rsync --archive --exclude="foo bar.txt"'
myrsync dir1 dir2
cbix
  • 141
0

One option is to use eval to parse into an array. Obviously this will let whoever controls the string doing whatever they want.

FLAGS='--archive --exclude="foo bar.txt"'
eval "FLAGS=($FLAGS)"
rsync "${FLAGS[@]}" dir1 dir2
Kevin Cox
  • 1,484
  • 1
  • 9
  • 12
0

As stated by @cbix, using an alias is a nice solution in this case.

Don't forget to allow aliases expansion in scripts by using the line

shopt -s expand_aliases

As explained here https://www.thegeekdiary.com/how-to-make-alias-command-work-in-bash-script-or-bashrc-file/ it is stated in the man page of bash:

Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt

fvlinden
  • 101
-2

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
  • 5
    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 02:25
  • @GordonDavisson But printargs() prints same output if no arguments or if one empty string argument. – JohnMudd Dec 17 '19 at 13:51