Bash command quoting issue

2020-02-02 00:38发布

问题:

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


标签: bash quoting