代码之家  ›  专栏  ›  技术社区  ›  Tom Hale

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

  •  0
  • Tom Hale  · 技术社区  · 7 年前

    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 回复  |  直到 7 年前
        1
  •  2
  •   Feng    7 年前

    因为变量 ZPLGM -H 选项。

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

    typeset 有一个选择 ,如手册所述:

      -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 )