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

zsh: `declare -p` associative array does not print values

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 打印一条在执行时会重新创建数组的语句?

因为变量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 )