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}
精彩评论