无法将函数参数分配给 `Zsh` 中的局部数组变量

Can not assign function parameters to local array variable in `Zsh`

我只是尝试将函数的参数分配为局部数组变量,我试过了

$test_print(){local foo=( "${@:1}" ); echo $foo[*]}; test_print a b c

我得到了

test_print: bad pattern: foo=( a

但是如果我删除 local 关键字

$test_print(){foo=( "${@:1}" ); echo $foo[*]}; test_print a b c

工作正常

a b c

这里有什么问题?如何将我的数组保存到局部变量?

附加信息

我在 bash shell 上试过了,它作为 localglobal 变量工作得很好。

为了进行想要的赋值,您必须将 foo 的声明和值的赋值分成两个命令:

test_print(){local foo; foo=( "${@:1}" ); echo $foo[*]}; test_print a b c

根据 ZSH Manual local 的行为类似于 typeset:

**local [ {+|-}AEFHUahlprtux ] [ -LRZi [ n ]] [ name[=value] ] ...

Same as typeset, except that the options -g, and -f are not permitted. In this case the -x option does not force the use of -g, i.e. exported variables will be local to functions.

typeset 的段落中说:

Note that arrays currently cannot be assigned in typeset expressions, only scalars and integers.