Bash command quoting issue

筅森魡賤 提交于 2019-11-26 23:37:27

问题


I have this weird issue, I can't understand why is happening. It should be a piece of cake for any bash ninja.

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

I expected this to be translated as...

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

...and just work. But it doesn't. When running the full command, I have no problems, unison is ok with that. But when I run the command unison $OPTIONS a b, unison complains:

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)

What am I doing wrong?


回答1:


BASH FAQ entry #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


来源:https://stackoverflow.com/questions/14564250/bash-command-quoting-issue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!