开发者

Bash: How do I create a variable containing a list of arguments with multi-word tokens?

I want to create a variable which is essentially a list of arguments to pass to a command or function. The pattern below with arg_string works well for foo, bar, and baz, bu开发者_如何学编程t not for "multi word token", which I would like the command to see as a single argument.

#!/bin/bash
function func() {
  for arg in "$@"
  do
    echo ${arg}
  done
}

arg_string="foo bar baz \"multi word token\""
arg_string="foo bar baz multi\ word\ token"
arg_string="foo bar baz 'multi word token'"
func ${arg_string}

Here is the output:

foo
bar
baz
'multi
word
token'

When I want:

foo
bar
baz
multi word token


Just stick an eval before the function call:

 eval func ${arg_string}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜