为什么 bash 参数扩展会导致 rsync 命令以不同方式运行?

Why do bash parameter expansions cause an rsync command to operate differently?

我正在尝试 运行 将文件复制到新位置的 rsync 命令。如果我直接 运行 rsync 命令,在命令行上没有任何参数扩展,rsync 会做我期望的事情

$ rsync -amnv --include='lib/***' --include='arm-none-eabi/include/***' \
  --include='arm-none-eabi/lib/***' --include='*/' --exclude='*' \
  /tmp/from/ /tmp/to/

building file list ... done
created directory /tmp/to
./
arm-none-eabi/
arm-none-eabi/include/
arm-none-eabi/include/_ansi.h
...
arm-none-eabi/lib/
arm-none-eabi/lib/aprofile-validation.specs
arm-none-eabi/lib/aprofile-ve.specs
...
lib/
lib/gcc/
lib/gcc/arm-none-eabi/
lib/gcc/arm-none-eabi/4.9.2/
lib/gcc/arm-none-eabi/4.9.2/crtbegin.o
...

sent 49421 bytes  received 6363 bytes  10142.55 bytes/sec
total size is 423195472  speedup is 7586.32 (DRY RUN)

但是,如果我将过滤器参数包含在一个变量中,并使用该变量调用命令,则会观察到不同的结果。 rsync 复制了一些我做的额外目录 not expect:

$ FILTER="--include='lib/***' --include='arm-none-eabi/include/***' \
  --include='arm-none-eabi/lib/***' --include='*/' --exclude='*'"
$ rsync -amnv ${FILTER} /tmp/from/ /tmp/to/

building file list ... done
created directory /tmp/to
./
arm-none-eabi/
arm-none-eabi/bin/
arm-none-eabi/bin/ar
...
arm-none-eabi/include/
arm-none-eabi/include/_ansi.h
arm-none-eabi/include/_syslist.h
...
arm-none-eabi/lib/
arm-none-eabi/lib/aprofile-validation.specs
arm-none-eabi/lib/aprofile-ve.specs
...
bin/
bin/arm-none-eabi-addr2line
bin/arm-none-eabi-ar
...
lib/
lib/gcc/
lib/gcc/arm-none-eabi/
lib/gcc/arm-none-eabi/4.9.2/
lib/gcc/arm-none-eabi/4.9.2/crtbegin.o
...

sent 52471 bytes  received 6843 bytes  16946.86 bytes/sec
total size is 832859156  speedup is 14041.53 (DRY RUN)

如果我 echo 失败的命令,它会生成成功的确切命令。复制输出,运行ning直接给了我预期的结果。

关于 bash 参数扩展的工作原理,我显然遗漏了一些东西。有人可以解释为什么这两个不同的调用会产生不同的结果吗?

shell 在扩展变量之前解析引号,因此将引号放入变量的值中并不会达到您的预期效果——当它们就位时,已经为时已晚了。有用。有关详细信息,请参阅 BashFAQ #50: I'm trying to put a command in a variable, but the complex cases always fail!

在您的情况下,解决此问题的最简单方法似乎是使用数组而不是纯文本变量。这样,引号在创建数组时被解析,每个 "word" 被存储为一个单独的数组元素,如果您正确引用变量(使用双引号和 [@]),数组元素在没有任何不需要的解析的情况下包含在命令的参数列表中:

filter=(--include='lib/***' --include='arm-none-eabi/include/***' \
  --include='arm-none-eabi/lib/***' --include='*/' --exclude='*')
rsync -amnv "${filter[@]}" /tmp/from/ /tmp/to/

请注意,数组在 bash 和 zsh 中可用,但并非所有其他 POSIX 兼容的 shells。另外,我将 filter 变量名小写——推荐的做法是避免与 shell 的特殊变量(全部大写)冲突。

为了方便起见,我喜欢将参数分成不同的行:

ROPTIONS=(
   -aNHXxEh
   --delete
   --fileflags
   --exclude-from=$EXCLUDELIST
   --delete-excluded
   --force-change
   --stats
   --protect-args
)

然后这样称呼它:

rsync "${ROPTIONS[@]}" "$SOURCE" "$DESTINATION"