简体   繁体   English

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

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

I just try to assign parameters of function as local array variable, I tried 我只是尝试将函数的参数分配为局部数组变量,

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

I got 我有

test_print: bad pattern: foo=( a

But if I remove local keyword 但是如果我删除local关键字

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

It's work 是工作

a b c

What is a problem here? 这里有什么问题? How can I keep my array to local variable? 如何将数组保留为局部变量?

Additional information 附加信息

I tried this on bash shell, it's work well either as local or global variable. 我在bash shell上进行了尝试,无论是local变量还是global变量,它bash很好地工作。

In order to make the wanted assignment, you have to separate the declaration of foo and the assignment of the value into two command: 为了进行所需的赋值,必须将foo的声明和值的赋值分成两个命令:

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

According to the ZSH Manual local behaves like typeset : 按照ZSH手册 local表现得像typeset

**local [ {+|-}AEFHUahlprtux ] [ -LRZi [ n ]] [ name[=value] ] ... **本地[{+ |-} AEFHUahlprtux] [-LRZi [n]] [名称[=值]] ...

Same as typeset, except that the options -g, and -f are not permitted. 与排版相同,但不允许选项-g和-f。 In this case the -x option does not force the use of -g, ie exported variables will be local to functions. 在这种情况下,-x选项不会强制使用-g,即,导出的变量对于函数而言是局部的。

In the the paragraph on typeset it says: typeset的段落中说:

Note that arrays currently cannot be assigned in typeset expressions, only scalars and integers. 请注意,当前无法在排版表达式中分配数组,只能分配标量和整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM