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

ste*_*ail 95 macos shell zsh

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

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

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

sim*_*ont 129

tl;博士版:使用 ~/.zshrc

阅读手册页以了解之间的区别:

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


关于我的评论

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

这似乎是不正确的 - /etc/profile未在我能找到的任何zsh文档中列出.

事实证明这部分是不正确的:/etc/profile 可能来自zsh.但是,只有在zsh"被调用为shksh" 时才会出现这种情况; 在这些兼容模式中:

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

ArchWiki ZSH链接说:

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

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



要解决这个问题:

我应该在哪里将我的rvm,python,节点等添加到我的$ PATH?

一般来说,我会导出我$PATH~/.zshrc,但它是值得拥有的读zshall手册页,特别是"启动/关闭文件"部分- ~/.zshrc阅读的互动炮弹,这可能会或可能不适合您的需求-如果你想$PATHzsh你调用的每个shell(无论是否interactive,不是,login不是等),~/.zshenv都是更好的选择.

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

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

.bashrc.bash_profile不是阅读zsh,除非你明确地从源头他们~/.zshrc或相似; 之间的语法bashzsh不是总是兼容.两者都是为设置.bashrc.bash_profile设计的bash,而不是zsh设置.

  • 谢谢你不仅仅是彻底回答simont.奇怪的是,你说`.bashrc`和`.bash_profile`不是由`zsh`读取的,因为我现在在.bashrc`中添加了`$ PATH`的``rvm`,而在`.bash_profile中添加了我的`python` `,两者都被添加得很好.在任何情况下,我都会将所有`$ PATH`导出移动到`〜/ .zshrc`,因为我的所有其他`zsh`配置都存放在那里.我不得不承认,我不太熟悉不同类型的贝壳.从阅读你发布的链接,我猜我使用的是交互式shell,但我会进一步阅读以确保...再次感谢! (3认同)

jor*_*jor 28

以下是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.
Run Code Online (Sandbox Code Playgroud)

从这里我们可以看到订单文件被读取是:

/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
Run Code Online (Sandbox Code Playgroud)

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


abe*_*312 14

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

解:


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

~/.zshrc
Run Code Online (Sandbox Code Playgroud)