命令重新分配

opy*_*ate 5 zsh function

我之前找到了一个 Bash 脚本片段,用于将字符串回显到 stderr:

echoerr() { echo "$@" 1>&2; }
echoerr hello world
Run Code Online (Sandbox Code Playgroud)

这保留在我的剪贴板中,当我想编辑一个文件(使用 VIM)时,我不小心再次粘贴了这个片段而不是文件名:

vim echoerr() { echo "$@" 1>&2; }
echoerr hello world
Run Code Online (Sandbox Code Playgroud)

它似乎已重新分配echoerrvim

$ where vim
vim () {
    echo "$@" 1>&2;
}
/usr/bin/vim
Run Code Online (Sandbox Code Playgroud)

此外,尝试使用 VIM 打开文件现在只会回显文件名:

vim path/to/some-file
Run Code Online (Sandbox Code Playgroud)

印刷:

path/to/some-file
Run Code Online (Sandbox Code Playgroud)

发生了什么?(我在 tmux 中运行 zsh)

cuo*_*glm 5

因为zsh允许您定义具有多个名称的函数。来自man zshmisc

function word ... [ () ] [ term ] { list }
       word ... () [ term ] { list }
       word ... () [ term ] command
              where term is one or more newline or ;.  Define a function which
              is referenced by any one of word.  Normally, only  one  word  is
              provided;  multiple  words  are  usually only useful for setting
              traps.  The body of the function is the list between the  {  and
              }.  See the section `Functions'.
Run Code Online (Sandbox Code Playgroud)