如何使用脚本访问用户的 fpath 而不是系统的 fpath?

如何使用脚本访问用户的 fpath 而不是系统的 fpath?

测试.zsh:

#!/usr/bin/env zsh                                                                                                           

for f in ${fpath}; do
    echo ${f}
done

输出:

/usr/local/share/zsh/site-functions
/usr/share/zsh/functions/Calendar
/usr/share/zsh/functions/Completion
/usr/share/zsh/functions/Completion/AIX
/usr/share/zsh/functions/Completion/BSD
/usr/share/zsh/functions/Completion/Base
/usr/share/zsh/functions/Completion/Cygwin
/usr/share/zsh/functions/Completion/Darwin
/usr/share/zsh/functions/Completion/Debian
/usr/share/zsh/functions/Completion/Linux
/usr/share/zsh/functions/Completion/Mandriva
/usr/share/zsh/functions/Completion/Redhat
/usr/share/zsh/functions/Completion/Solaris
/usr/share/zsh/functions/Completion/Unix
/usr/share/zsh/functions/Completion/X
/usr/share/zsh/functions/Completion/Zsh
/usr/share/zsh/functions/Exceptions
/usr/share/zsh/functions/MIME
/usr/share/zsh/functions/Misc
/usr/share/zsh/functions/Newuser
/usr/share/zsh/functions/Prompts
/usr/share/zsh/functions/TCP
/usr/share/zsh/functions/VCS_Info
/usr/share/zsh/functions/VCS_Info/Backends
/usr/share/zsh/functions/Zftp
/usr/share/zsh/functions/Zle

虽然命令行输出有一些额外的条目:

╭─ting@core ~/code/autojump  ‹install-zsh-local*›
╰─➤  echo $fpath       
/home/ting/code/oh-my-zsh/plugins/git
/home/ting/code/oh-my-zsh/functions
/home/ting/code/oh-my-zsh/completions
/usr/local/share/zsh/site-functions
/usr/share/zsh/functions/Calendar
/usr/share/zsh/functions/Completion
/usr/share/zsh/functions/Completion/AIX
/usr/share/zsh/functions/Completion/BSD
/usr/share/zsh/functions/Completion/Base
/usr/share/zsh/functions/Completion/Cygwin
/usr/share/zsh/functions/Completion/Darwin
/usr/share/zsh/functions/Completion/Debian
/usr/share/zsh/functions/Completion/Linux
/usr/share/zsh/functions/Completion/Mandriva
/usr/share/zsh/functions/Completion/Redhat
/usr/share/zsh/functions/Completion/Solaris
/usr/share/zsh/functions/Completion/Unix
/usr/share/zsh/functions/Completion/X
/usr/share/zsh/functions/Completion/Zsh
/usr/share/zsh/functions/Exceptions
/usr/share/zsh/functions/MIME
/usr/share/zsh/functions/Misc
/usr/share/zsh/functions/Newuser
/usr/share/zsh/functions/Prompts
/usr/share/zsh/functions/TCP
/usr/share/zsh/functions/VCS_Info
/usr/share/zsh/functions/VCS_Info/Backends
/usr/share/zsh/functions/Zftp
/usr/share/zsh/functions/Zle

如何让脚本访问用户的 fpath 而不是系统的?

答案1

您实际上无法做到这一点,因为用户fpath不是通过键值配置文件中的简单分配来设置的,而是通过潜在复杂的脚本来设置的。例如,myfpath会根据 zsh 的版本以及系统上存在的目录而有所不同。您可以获取用户的~/.zshrc,但这可能会执行各种其他操作(特别是如果 zsh 未在终端中运行,则可能无法工作,并且可能会打印内容,否则假设 shell 是交互式 shell)。

zsh source 的所有实例~/.zshenv,因此建议用户fpath从那里进行设置。

相关内容