Bash 将包含嵌入空格和引号的文件的多行shell变量指定为rsync命令的参数

Bash 将包含嵌入空格和引号的文件的多行shell变量指定为rsync命令的参数,bash,shell,variables,arguments,quoting,Bash,Shell,Variables,Arguments,Quoting,SO上的所有其他解决方案都适用于echo和类似命令。但是,我尝试将一长串参数(分割成多行)分配给单个变量,该变量将扩展为rsync命令的多个参数。rsync命令(或bash的空白标记化)在大多数推荐的解决方案中都不起作用 SYNC_OPTIONS="-a --progress --delete --delete-before --delete-excluded" SYNC_EXCLUDE=" --exclude='some/dir/with spaces/requiring quotin

SO上的所有其他解决方案都适用于
echo
和类似命令。但是,我尝试将一长串参数(分割成多行)分配给单个变量,该变量将扩展为rsync命令的多个参数。rsync命令(或bash的空白标记化)在大多数推荐的解决方案中都不起作用

SYNC_OPTIONS="-a --progress --delete --delete-before --delete-excluded"
SYNC_EXCLUDE="
    --exclude='some/dir/with spaces/requiring quoting'
    --exclude='another/dir/with spaces'
    --exclude='blah blah/blah'
    "
echo rsync ${SYNC_OPTIONS} ${SYNC_EXCLUDE} /src/ /dst/
set -x
rsync ${SYNC_OPTIONS} ${SYNC_EXCLUDE} /src/ /dst/
set +x
产生:

$ echo rsync ${SYNC_OPTIONS} ${SYNC_EXCLUDE} /src/ /dst/
rsync -a --progress --delete --delete-before --delete-excluded --exclude='some/dir/with spaces/requiring quoting' --exclude='another/dir/with spaces' --exclude='blah blah/blah' /src/ /dst/
$ set -x
$ rsync ${SYNC_OPTIONS} ${SYNC_EXCLUDE} /src/ /dst/
+ rsync -a --progress --delete --delete-before --delete-excluded '--exclude='\''some/dir/with' spaces/requiring 'quoting'\''' '--exclude='\''another/dir/with' 'spaces'\''' '--exclude='\''blah' 'blah/blah'\''' /src/ /dst/
rsync: change_dir "/spaces" failed: No such file or directory (2)
rsync: link_stat "/quoting'" failed: No such file or directory (2)
rsync: link_stat "/spaces'" failed: No such file or directory (2)
rsync: change_dir "/blah" failed: No such file or directory (2)
跟踪输出和rsync错误消息都表明存在一些额外的、不需要的标记化级别,这破坏了rsync命令中变量的使用。但是,
echo
命令的输出看起来是正确的


如何定义多行变量,该变量将用作rsync命令的参数,其中该参数包含带空格的引用文件路径?具体地说,我对
SYNC\u EXCLUDE
变量的上述情况感兴趣。

最终找到了包含在冗长但很好的答案中的答案。在我的例子中,我将变量转换为字符串数组,而不是单个长字符串,如下所示:

SYNC_OPTIONS="-a --progress --delete --delete-before --delete-excluded"
SYNC_EXCLUDE=(
    --exclude='some/dir/with spaces/requiring quoting'
    --exclude='another/dir/with spaces'
    --exclude='blah blah/blah'
    )
echo rsync ${SYNC_OPTIONS} "${SYNC_EXCLUDE[@]}" /src/ /dst/
set -x
rsync ${SYNC_OPTIONS} "${SYNC_EXCLUDE[@]}" /src/ /dst/
set +x
和的可能副本。