macos - 在 zsh 中哪里放置 $PATH 变量断言?

标签 macos shell zsh

我喜欢 zsh,但我不确定将我的 $PATH 和其他变量断言放在哪里?我发现它们分散在文件 .zshrc .zprofile .bashrc .bash_profile 之间,有时会加倍。

我意识到,在 bash 文件中包含任何内容并没有多大意义,因为我正在使用 zsh,但是我到底应该将其放置在哪里我的 rvmpythonnode 等添加到我的 $PATH 中?

是否有我应该使用的特定文件(即 .zshenv,它当前存在于我的安装中),我当前正在使用的文件之一,或者这还重要吗?

最佳答案

tl;dr版本:使用~/.zshrc

read the man page了解之间的差异:

~/.zshrc, ~/.zshenv and ~/.zprofile.

<小时/>

关于我的评论

在我附加到答案的评论中kev gave ,我说:

This seems to be incorrect - /etc/profile isn't listed in any zsh documentation I can find.

这被证明是部分不正确的:/etc/profile可能zsh获取。 但是,只有当 zsh 被“作为 shksh 调用”时才会发生这种情况;在这些兼容模式下:

The usual zsh startup/shutdown scripts are not executed. Login shells source /etc/profile followed by $HOME/.profile. If the ENV environment variable is set on invocation, $ENV is sourced after the profile scripts. The value of ENV is subjected to parameter expansion, command substitution, and arithmetic expansion before being interpreted as a pathname. [man zshall, "Compatibility"].

ArchWiki ZSH link说:

At login, Zsh sources the following files in this order:
/etc/profile
This file is sourced by all Bourne-compatible shells upon login

这意味着 /etc/profile 在登录时zsh总是读取 - 我没有任何使用 Arch Linux 的经验项目; wiki 对于该发行版可能是正确的,但通常是正确的。与 zsh 手册页相比,该信息不正确,并且似乎不适用于 OS X 上的 zsh($PATH 中的路径在 /etc/中设置) profile 不会进入我的 zsh session )。

<小时/>


解决这个问题:

where exactly should I be placing my rvm, python, node etc additions to my $PATH?

通常,我会从 ~/.zshrc 导出我的 $PATH,但值得一读 zshall手册页,特别是“启动/关闭文件”部分 - ~/.zshrc 是针对交互式 shell 读取的,它可能适合也可能不适合您的需求 - 如果您想要$PATH 用于您调用的每个 zsh shell(interactive 和非 login 等) ,那么 ~/.zshenv 是一个更好的选择。

Is there a specific file I should be using (i.e. .zshenv which does not currently exist in my installation), one of the ones I am currently using, or does it even matter?

启动时会读取一堆文件(检查链接的手册页),这是有原因的 - 每个文件都有其特定的位置(每个用户的设置、特定于用户的设置) 、登录 shell 的设置、每个 shell 的设置等)。
不用担心 ~/.zshenv 不存在 - 如果您需要它,创建它,它将被读取。

.bashrc.bash_profile 不会被 zsh 读取,除非您明确从 获取它们>~/.zshrc 或类似的; bashzsh 之间的语法并不始终兼容。 .bashrc.bash_profile 都是为 bash 设置而不是 zsh 设置而设计的。

关于macos - 在 zsh 中哪里放置 $PATH 变量断言?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10574684/

相关文章:

macos - Homebrew 的 `git` 未使用完成

macos - 在 NSTableView 中选择行时收到通知

shell - 按数字顺序并排粘贴多个文件

linux - 我想用单独的文件夹存储最近 10 天的数据库备份

command-prompt - 如何在 Zsh 提示符中显示电池状态

c++ - 为什么我的 shell 只检测 0 - 255 之间的返回码?

css - 在我的 Mac 上全局安装 SASS 时出错

python - 将 Tkinter GUI 卸载到 Mac OS 上的另一个线程会产生挂起的窗口

Java - Runtime.exec() 无法运行

java - 每 5 分钟间隔使用 grep 解析日志文件