如何使用set来设置shell函数的复杂参数

时间:2015-02-16 15:39:19

标签: linux shell set ash

我想执行一个带有复杂参数的函数

我想以这种方式输入它们(如果可能的话?)

set [-options here] a111 "a222" "a333 a333" "a444"
set [-options here] b111 "b222" "b333 b333" "b444"
set [-options here] c111 "c222" "c333 c333" "c444"

myfunc "$@"

myfunc应该看

a111 "a222" "a333 a333" "a444" ==> as the first argument with keeping ""
b111 "b222" "b333 b333" "b444" ==> as the second argument with keeping ""
c111 "c222" "c333 c333" "c444" ==> as the third argument with keeping ""

是否可以使用set或其他方式执行此操作?以及如何做到这一点?

顺便说一下,我不想使用\"

此外,我无法使用'(例如'"a333 a333"'),因为它不评估变量

1 个答案:

答案 0 :(得分:1)

如果你想让值得到引号(这通常是一件奇怪的事情),你需要将它们从shell中转义。

$ set -- a111 '"a222"' '"a333 a333"' "\"a444\""
$ c() {
printf argc:%s\\n "$#"
printf argv:%s\\n "$@"
}
$ c "$@"
argc:4
argv:a111
argv:"a222"
argv:"a333 a333"
argv:"a444"