我无法autoload在zsh中找到广泛使用的命令的文档.有人能用简单的英语解释吗?
更具体一点:模块的自动加载意味着什么,例如在这一行中:
autoload -Uz vcs_info
它有什么作用?
我试过autoload --help,man autoload谷歌搜索 - 没有成功.谢谢!
cda*_*rke 25
该autoload功能在bash中不可用,但它位于ksh(korn shell)和zsh.上zsh见man zshbuiltins.
以与任何其他命令相同的方式调用函数.程序和函数之间可能存在名称冲突.什么autoload做的是,以纪念该名称作为一个函数,而不是一个外部程序.该函数必须独立于一个文件中,文件名与函数名相同.
autoload -Uz vcs_info
Run Code Online (Sandbox Code Playgroud)
该-U方法标记vcs_info自动加载功能并抑制别名扩展.该-z方法使用zsh(而不是ksh)的风格.另请参见functions命令.
有关更多详细信息,请参阅http://zsh.sourceforge.net/Doc/Release/Functions.html.
Mar*_*eed 18
autoload告诉 zsh 在$FPATH/$fpath中查找包含函数定义的文件,而不是$PATH/ 中$path包含脚本(或二进制可执行文件)的文件。
脚本只是在脚本运行时执行的一系列命令。例如,假设您有一个名为 的文件hello:
echo "Setting 'greeting'"
greeting='Hello'
Run Code Online (Sandbox Code Playgroud)
Scripts get their own copy of the shell process, so anything they do can't have any side effects on the calling shell environment. The assignment to greeting above will be in effect only within the script; once it exits, it won't have had any impact on your interactive shell session:
$ hello
Setting 'greeting'
$ echo $greeting
$
Run Code Online (Sandbox Code Playgroud)
Functions are defined once and live in the shell's memory; when you call them, they excecute inside your shell environment and can therefore have side effects:
hello() {
echo "Setting 'greeting'"
greeting='Hello'
}
$ hello
Setting 'greeting'
$ echo $greeting
Hello
Run Code Online (Sandbox Code Playgroud)
So you use functions when you want to modify your shell environment. The Zsh Line Editor (ZLE) also uses functions - when you bind a key to some action, that action is defined as a shell function (which has to be added to ZLE with the zle -N command.)
Now, if you have a lot of functions, then you might not want to define all of them in your .zshrc every time you start a new shell; that slows down shell startup and uses memory to store functions that you might not wind up calling during the lifetime of that shell. So you can instead put the function definitions into their own files, named after the functions they define, and put the files into directories in your $FPATH, which works like $PATH. Zsh comes with a bunch of standard functions in the default $FPATH already. But it won't know to look for a command there unless you've first told it that the command is a function. That's essentially what autoload does; it says "Hey, Zsh, this command name here is a function, so when I try to run it, go look for its definition in my FPATH instead of looking for a command in my PATH."
Worth noting is that the first time you run an autoloaded function, Zsh sources the definition file, but – unlike ksh - it does not then call the function. It's up to you to call the function inside the file after defining it so that first invocation will work. An autoloadable definition of hello might look like this:
hello() {
echo "Setting 'greeting'"
greeting='Hello'
}
hello "$@"
Run Code Online (Sandbox Code Playgroud)