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
Where to place $PATH variable assertions in zsh?
提问by stefmikhail
I love zsh
, but I am not sure where to place my $PATH
and 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 bash
files doesn't make much sense seeing as I'm using zsh
, but where exactly should I be placing my rvm
, python
, node
etc additions to my $PATH
?
我认识到,具有内部的任何bash
文件,没有多大意义看到,因为我使用的zsh
,但正是我应该把我rvm
,python
,node
等添加到我的$PATH
?
Is there a specific file I should be using (i.e. .zshenv
which 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
,~/.zshenv
and~/.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/profile
maybe sourced by zsh
. However, this only occurs if zsh
is "invoked as sh
or ksh
"; in these compatibility modes:
事实证明这是部分不正确的:/etc/profile
可能来自zsh
. 但是,这仅在zsh
“调用为sh
或ksh
”时发生;在这些兼容模式下:
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:
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/profile
is always readby zsh
at 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 $PATH
set in /etc/profile
do 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 $PATH
from ~/.zshrc
, but it's worth having a read of the zshallman page, specifically the "STARTUP/SHUTDOWN FILES" section - ~/.zshrc
is read for interactiveshells, which may or may not suit your needs - if you want the $PATH
for every zsh
shell invoked by you (both interactive
and not, both login
and not, etc), then ~/.zshenv
is a better option.
通常,我会导出我的$PATH
from ~/.zshrc
,但值得阅读zshall手册页,特别是“启动/关闭文件”部分 -~/.zshrc
用于交互式shell,这可能适合也可能不适合您的需要 - 如果您想要$PATH
forzsh
您调用的每个外壳程序(两者都有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 man
pages), 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 ~/.zshenv
not existing - if you need it, make it, and it will be read.
启动时读取了一堆文件(检查链接man
页面),这是有原因的 - 每个文件都有其特定的位置(每个用户的设置,特定于用户的设置,登录外壳的设置,每个外壳的设置,等等)。
不要担心~/.zshenv
不存在 - 如果您需要它,请制作它,它会被阅读。
.bashrc
and .bash_profile
are notread by zsh
, unless you explicitly source them from ~/.zshrc
or similar; the syntax between bash
and zsh
is notalways compatible. Both .bashrc
and .bash_profile
are designed for bash
settings, not zsh
settings.
.bashrc
而.bash_profile
在不被读取zsh
,除非你明确地从源头他们~/.zshrc
或相似; 之间的语法bash
和zsh
是不是总是兼容。这两个.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