NB: though the question below features rsync, it is not a question about rsync; it is a question about zsh arrays.
If I initialize the variable EXCLUDES like this
EXCLUDES=( --exclude=/foo --exclude=/bar --exclude=/baz )
then, when I run the command
rsync -a $EXCLUDES / /some/target
...then I see that /foo, /bar, and /baz are indeed not copied to /some/target.
But now, suppose that EXCLUDE_ITEMS is something like this
EXCLUDE_ITEMS=( /foo /bar /baz )
...and I initialize EXCLUDE like this
EXCLUDES=()
for item in $EXCLUDE_ITEMS
do
EXCLUDES+=( "--exclude='$item'" )
done
...or like this
EXCLUDES=( $(for item in $EXCLUDE_ITEMS; do echo "--exclude='$item'"; done) )
...then in either case, after I run
rsync -a $EXCLUDES / /some/target
...I now find that the excluded directories have not been excluded from the transfer.
Since the command lines (as typed on the terminal) are all identical, I must conclude that there is a difference between the explicitly initialized EXCLUDES from those that get initialized by iterating over EXCLUDE_ITEMS, but I cannot figure out what it is.
How can I initialize EXCLUDE from the items in EXCLUDE_ITEMS so that the latter indeed get excluded when I run
rsync -a $EXCLUDES / /some/target
PS: In fact, if I run
eval "rsync -a $EXCLUDES / /some/target"
...where EXCLUDES has been initialized in either of the for-loop-based ways shown above, then the directories named in EXCLUDES are indeed excluded, as desired.