gpt4 book ai didi

zsh - Z Shell "autoload"内置 - 它有什么用?

转载 作者:行者123 更新时间:2023-12-03 11:00:41 30 4
gpt4 key购买 nike

我已经使用 Z shell 有一段时间了,我开始感到好奇。我在编写自己的函数时偶然发现的一件事是“自动加载”。

根据 zshbuiltins(1) 手册页 autoload是“相当于 functions -u”(有异常(exception)),也就是“相当于 typeset -f”(有异常(exception))。但是,在查看自动加载使用后,说 functions/Prompts/promptinit ,我想我知道它的作用了。

我认为自动加载是一种“导入”语句。

但是为什么“autoload foo”优于“source bar”?我不明白。

最佳答案

zsh documentation 中所述:

A function can be marked as undefined using the autoload builtin (or functions -u or typeset -fu). Such a function has no body. When the function is first executed, the shell searches for its definition using the elements of the fpath variable. [...]



autoload 允许在没有主体的情况下指定函数,然后在使用时自动加载;)

source 但是将脚本作为参数,然后是 executed在当前 session 的环境中 - 即您将保留脚本对环境所做的所有更改,而仅执行脚本时并非如此。

我认为这个功能在拥有 时很有用手数函数中的实用程序。它允许更快的启动(不需要加载自动加载函数的所有代码)并且可以保持 shell 的内存占用更小。

关于zsh - Z Shell "autoload"内置 - 它有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4493173/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com