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
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 prefix) is impossible, right?
– mvorisek
Nov 15 '20 at 23:18
eval or equivalent), then it's impossible.
– Gordon Davisson
Nov 16 '20 at 05:17
eval is another option:
$ x="'a b'"
$ printf '%s,%s' $x
'a,b'
$ eval printf '%s,%s' $x
a b
See also:
Alternatively, you can create an alias for your command:
alias myrsync='rsync --archive --exclude="foo bar.txt"'
myrsync dir1 dir2
alias myrsync="rsync $FLAGS" is more correct answer to the specific question IMO
– Martian2020
Dec 21 '22 at 03:59
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
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
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
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