macos 在 zsh 中放置 $PATH 变量断言的位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10574684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 09:09:43  来源:igfitidea点击:

Where to place $PATH variable assertions in zsh?

macosshellzsh

提问by stefmikhail

I love zsh, but I am not sure where to place my $PATHand other variable assertions? I find that they are scattered between the files .zshrc.zprofile.bashrc.bash_profile, and sometimes doubled.

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

I realize that having anything inside the bashfiles doesn't make much sense seeing as I'm using zsh, but where exactly should I be placing my rvm, python, nodeetc additions to my $PATH?

我认识到,具有内部的任何bash文件,没有多大意义看到,因为我使用的zsh,但正是我应该把我rvmpythonnode等添加到我的$PATH

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

有没有我应该使用(即一个特定的文件.zshenv,其不会没有目前在我的安装存在),我目前使用的手机之一,或者它甚至重要吗?

回答by simont

tl;dr version: use ~/.zshrc

TL;博士版本:使用 ~/.zshrc

And read the man pageto understand the differences between:

阅读手册页以了解以下区别:

~/.zshrc, ~/.zshenvand ~/.zprofile.

~/.zshrc~/.zshenv~/.zprofile



Regarding my comment

关于我的评论

In my comment attached to the answer kev gave, I said:

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

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

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

This turns out to be partially incorrect: /etc/profilemaybe sourced by zsh. However, this only occurs if zshis "invoked as shor ksh"; in these compatibility modes:

事实证明这是部分不正确的:/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"].

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

The ArchWiki ZSH linksays:

ArchWiki ZSH链接说:

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

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

This implys that /etc/profileis always readby zshat login - I haven't got any experience with the Arch Linux project; the wiki may be correct for that distribution, but it is notgenerally correct. The information isincorrect compared to the zsh manual pages, and doesn't seem to apply to zsh on OS X (paths in $PATHset in /etc/profiledo not make it to my zsh sessions).

这implys那/etc/profile永远读通过zsh在登录时-我没有与Arch Linux的项目的经验; wiki 可能对那个分布是正确的,但它通常正确。该信息比较zsh的手册页不正确的,似乎并不适用于OS X上的zsh(在路径$PATH中集/etc/profile不使其向我的zsh会话)。





To address the question:

要解决这个问题:

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

我到底应该把我的 rvm、python、node 等添加到我的 $PATH 的什么位置?

Generally, I would export my $PATHfrom ~/.zshrc, but it's worth having a read of the zshallman page, specifically the "STARTUP/SHUTDOWN FILES" section - ~/.zshrcis read for interactiveshells, which may or may not suit your needs - if you want the $PATHfor every zshshell invoked by you (both interactiveand not, both loginand not, etc), then ~/.zshenvis a better option.

通常,我会导出我的$PATHfrom ~/.zshrc,但值得阅读zshall手册页,特别是“启动/关闭文件”部分 -~/.zshrc用于交互式shell,这可能适合也可能不适合您的需要 - 如果您想要$PATHforzsh您调用的每个外壳程序(两者都有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?

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

There's a bunch of files read on startup (check the linked manpages), and there's a reason for that - each file has it's particular place (settings for every user, settings for user-specific, settings for login shells, settings for every shell, etc).
Don't worry about ~/.zshenvnot existing - if you need it, make it, and it will be read.

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

.bashrcand .bash_profileare notread by zsh, unless you explicitly source them from ~/.zshrcor similar; the syntax between bashand zshis notalways compatible. Both .bashrcand .bash_profileare designed for bashsettings, not zshsettings.

.bashrc.bash_profile被读取zsh,除非你明确地从源头他们~/.zshrc或相似; 之间的语法bashzsh不是总是兼容。这两个.bashrc.bash_profile设计用于bash设置,而不是zsh设置。

回答by jor

Here is the docs from the zsh man pages under STARTUP/SHUTDOWN FILES section.

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

   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.

From this we can see the order files are read is:

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

/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

You can get more information here.

您可以在此处获取更多信息。

回答by abe312

I had similar problem (in bash terminal command was working correctly but zsh showed command not found error)

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

Solution:

解决方案:


just paste whatever you were earlier pasting in ~/.bashrc to:


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

~/.zshrc