zsh:`declare -p`关联数组不打印值

时间:2018-09-29 06:39:32

标签: zsh associative-array

run-help typeset说:

  -p [ n ]                                                                                                                                                                                                    
          If  the  -p  option  is  given, parameters and values are                                                                                                                                            
          printed in the form of a typeset command with an  assign-                                                                                                                                            
          ment,  regardless  of other flags and options.  Note that                                                                                                                                            
          the -H flag on parameters is respected; no value will  be                                                                                                                                            
          shown for these parameters.                           

请注意上面说parameters and values

如果这样做:

% typeset -p ZPLGM 
typeset -A ZPLGM

请注意,上面没有键值,但是它们确实存在:

% echo $ZPLGM[PLUGINS_DIR]
/home/ravi/.config/zsh/.zplugin/plugins
  1. 为什么typeset -p不能正常工作?
  2. 如何获取typeset来打印一条语句,该语句在执行时将重新创建数组?

1 个答案:

答案 0 :(得分:1)

因为变量ZPLGM是用-H选项定义的。

unset foo
typeset -AH foo=([bar]=123)
#         ^----here
echo $foo[bar]
typeset -p foo
123
typeset -A foo

typeset有一个选项-H,如手册所述:

  -H     Hide  value:  specifies that typeset will not display the
         value of the parameter when listing parameters; the  dis-
         play for such parameters is always as if the `+' flag had
         been given.  Use of the parameter is  in  other  respects
         normal, and the option does not apply if the parameter is
         specified by name, or by  pattern  with  the  -m  option.
         This   is  on  by  default  for  the  parameters  in  the
         zsh/parameter and zsh/mapfile  modules.   Note,  however,
         that  unlike the -h flag this is also useful for non-spe-
         cial parameters.
unset foo
typeset -A foo=([bar]=123)
echo $foo[bar]
typeset -p foo
123
typeset -A foo=( [bar]=123 )