即使使用选项 `complete_aliases` zsh 别名也不会扩展

zsh aliases don't expand even with option `complete_aliases`

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

% zsh -c 'setopt aliases complete_aliases; alias d=date; d'
zsh:1: command not found: d

使用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.