Bash命令引用问题

时间:2013-01-28 14:31:10

标签: bash quoting

我有这个奇怪的问题,我无法理解为什么会这样。任何bash忍者应该是小菜一碟。

OPTIONS="-auto -batch -ignore 'Path one' -ignore 'Path two' -ignore 'Path three'"
unison $OPTIONS a b

我希望将其翻译成......

unison -auto -batch -ignore 'Path one' -ignore 'Path two' -ignore 'Path three' a b

......只是工作。但事实并非如此。当运行完整命令时,我没有问题,一致就是这样。但是当我运行命令unison $OPTIONS a b时,unison抱怨:

Usage: unison [options]
    or unison root1 root2 [options]
    or unison profilename [options]

For a list of options, type "unison -help".
For a tutorial on basic usage, type "unison -doc tutorial".
For other documentation, type "unison -doc topics".

unison was invoked incorrectly (too many roots)

我做错了什么?

1 个答案:

答案 0 :(得分:7)

BASH FAQ#50:"I'm trying to put a command in a variable, but the complex cases always fail!"

options=(-auto -batch -ignore 'Path one' ...)
unison "${options[@]}" a b