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

zsh aliases即使使用“complete\u aliases”选项也不会展开`

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

    使用时如何扩展定义的别名 zsh -c ?

    % zsh -c 'setopt aliases complete_aliases; alias d=date; d'
    zsh:1: command not found: d
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Tom Hale    6 年前

    eval 迫使 zsh 解析别名 之后 代码已读入:

    zsh -c 'setopt aliases complete_aliases; alias d=date; eval d'
    

    d 不会扩展,所以不需要引用。

    一般来说你应该 properly quote all arguments to eval


    man zshmisc

       There is a commonly encountered problem with aliases illustrated by the
       following code:
    
            alias echobar='echo bar'; echobar
    
       This prints a message that the command echobar could not be found.
       This happens because aliases are expanded when the code is read in; the
       entire line is read in one go, so that when echobar is executed it is
       too late to expand the newly defined alias.  This is often a problem in
       shell scripts, functions, and code executed with `source' or `.'.
       Consequently, use of functions rather than aliases is recommended in
       non-interactive code.