【问题标题】:Where to place $PATH variable assertions in zsh?在 zsh 中哪里放置 $PATH 变量断言?
【发布时间】:2012-05-21 10:14:56
【问题描述】:

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

我意识到在 bash 文件中包含任何内容没有多大意义,因为我使用的是 zsh,但是 我应该将我的 rvm、python、@ 放在哪里987654331@ 等添加到我的$PATH?

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

【问题讨论】:

    标签: macos shell zsh


    【解决方案1】:

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

    和read the man page了解区别:

    ~/.zshrc、~/.zshenv 和 ~/.zprofile。


    关于我的评论

    在我对答案kev gave 的评论中,我说:

    这似乎不正确 - 我能找到的任何 zsh 文档中都没有列出 /etc/profile。

    事实证明这部分不正确:/etc/profile 可能来自zsh。 然而,只有当zsh 被“调用为sh 或ksh”时才会发生这种情况;在这些兼容模式下:

    不执行通常的 zsh 启动/关闭脚本。登录 shell 源 /etc/profile 后跟 $HOME/.profile。如果在调用时设置了 ENV 环境变量,则 $ENV 源自配置文件脚本。 ENV 的值在被解释为路径名之前经过参数扩展、命令替换和算术扩展。 [man zshall, "Compatibility"]。

    ArchWiki ZSH link 说:

    登录时,Zsh 按以下顺序获取以下文件:
    /etc/profile
    该文件在登录时由所有与 Bourne 兼容的 shell 提供

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



    解决问题:

    我应该将我的 rvm、python、node 等添加到我的 $PATH 的确切位置?

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

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

    在启动时读取了一堆文件(检查链接的 man 页面),这是有原因的 - 每个文件都有其特定的位置(每个用户的设置,用户特定的设置,登录 shell 的设置,每个外壳的设置等)。
    不要担心~/.zshenv 不存在 - 如果您需要,请制作它,它会被阅读。

    .bashrc 和 .bash_profile 不被zsh 读取不,除非您明确地从~/.zshrc 或类似来源获得它们; bash 和 zsh 之间的语法不总是兼容的。 .bashrc 和 .bash_profile 都是为 bash 设置设计的,而不是 zsh 设置。

    【讨论】:

    • 感谢您对西蒙的全面回答。奇怪的是,您说zsh 没有读取.bashrc 和.bash_profile,因为我目前在.bashrc 中添加了$PATH 和.bash_profile 中的rvm,以及在.bash_profile 中添加的python,两者都是被添加就好了。无论如何,我将把我所有的$PATH 导出移动到~/.zshrc,因为我所有的其他zsh 配置都存放在那里。我不得不承认,我对不同类型的贝壳不太熟悉。通过阅读您发布的链接,我猜我使用的是交互式外壳,但我会进一步阅读以确保...再次感谢!
    • 我在 Ubuntu 20.04 上的 zsh $PATH 出现问题,结果证明 ~/.bashrc 在我的登录 shell 中被我的 ~/.profile 读取。签入~/.profile,因为读取~/.bashrc是if [ -n "$BASH_VERSION" ],由于某种原因是我的zsh设置的,所以条件为真。
    【解决方案2】:

    这是来自 zsh 手册页的 STARTUP/SHUTDOWN FILES 部分的文档。

       Commands  are  first  read from /etc/zshenv this cannot be overridden.
       Subsequent behaviour is modified by the RCS and GLOBAL_RCS options; the
       former  affects all startup files, while the second only affects global
       startup files (those shown here with an path starting with  a  /).   If
       one  of  the  options  is  unset  at  any point, any subsequent startup
       file(s) of the corresponding type will not be read.  It is also  possi-
       ble  for  a  file  in  $ZDOTDIR  to  re-enable GLOBAL_RCS. Both RCS and
       GLOBAL_RCS are set by default.
    
       Commands are then read from $ZDOTDIR/.zshenv.  If the shell is a  login
       shell,  commands  are  read from /etc/zprofile and then $ZDOTDIR/.zpro-
       file.  Then, if the  shell  is  interactive,  commands  are  read  from
       /etc/zshrc  and then $ZDOTDIR/.zshrc.  Finally, if the shell is a login
       shell, /etc/zlogin and $ZDOTDIR/.zlogin are read.
    

    由此我们可以看出读取的订单文件是:

    /etc/zshenv    # Read for every shell
    ~/.zshenv      # Read for every shell except ones started with -f
    /etc/zprofile  # Global config for login shells, read before zshrc
    ~/.zprofile    # User config for login shells
    /etc/zshrc     # Global config for interactive shells
    ~/.zshrc       # User config for interactive shells
    /etc/zlogin    # Global config for login shells, read after zshrc
    ~/.zlogin      # User config for login shells
    ~/.zlogout     # User config for login shells, read upon logout
    /etc/zlogout   # Global config for login shells, read after user logout file
    

    您可以here获取更多信息。

    【讨论】:

      【解决方案3】:

      我有类似的问题(在 bash 终端命令工作正常但 zsh 显示命令未找到错误)

      解决方案:


      只需将您之前在 ~/.bashrc 中粘贴的内容粘贴到:

      ~/.zshrc
      

      【讨论】:

      • 奇怪的是,在 Mac 上,zsh 是默认的 shell,没有 .zshrc,只有 .zprofile。大多数 mac 工具和 brew 安装的实用程序仍然将配置放在 .bashrc 中,因此您必须手动复制它们。但是,从 .zprofile 末尾运行 .bashrc 会更容易
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-07
      • 1970-01-01
      • 2017-10-08
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多