简体   繁体   English

Zsh 中是否有类似 .bash_profile 的内容?

[英]Is there anything in Zsh like .bash_profile?

每次打开终端时,我都必须获取.bash_profile以启用$JAVA_HOME或其他变量。

Yes, it's called ~/.zshenv .是的,它被称为~/.zshenv

Here's how I have $JAVA_HOME set in ~/.zshenv :这是我在~/.zshenv中设置$JAVA_HOME的方式:

export JAVA_HOME="$(/usr/libexec/java_home)"

Keep in mind, however, that zsh is not bash, so just 'cause you have to source your .bash_profile every time you open a terminal does not mean that you have to do that with zsh.但是请记住,zsh不是bash,所以只是因为每次打开终端时都必须获取.bash_profile并不意味着您必须使用 zsh 来执行此操作。 With zsh, I only have to re-source my ~/.zshenv when I make changes to it , and then only for terminals which are already open : new terminals should have already sourced my new and improved ~/.zshenv .使用 zsh,我只需要在对它进行更改时重新获取我的~/.zshenv ,然后只针对已经打开的终端:新终端应该已经获取了我的新的和改进的~/.zshenv

NOTE笔记

I often find it helpful, when trying to determine which of my zsh startup files I should place things in to consult zsh startup files .我经常发现它很有帮助,当我试图确定我应该放置哪些 zsh 启动文件以查阅zsh 启动文件时。

A newer version of the documentation for startup files can be found here .可以在此处找到更新版本的启动文件文档。

I know this is an old question, but I recently upgraded MacOs to Catalina which changed the default shell from bash to zsh.我知道这是一个老问题,但我最近将 MacOs 升级到了 Catalina,它将默认 shell 从 bash 更改为 zsh。

I ended up doing this:我最终这样做了:

 echo source ~/.bash_profile > ~/.zshenv && source ~/.zshenv

To have zsh source my original .bash_profile.让 zsh 获取我原来的 .bash_profile。

Recently, with the upgrade to macOS Catalina, the default shell changed to zsh, which uses ~/.zshrc as the resource file.最近随着升级到 macOS Catalina,默认的 shell 改成了 zsh,它使用~/.zshrc作为资源文件。

We usually had ~/.bash_profile inside user home directory the solution is to simply我们通常在用户主目录中有~/.bash_profile解决方案是简单地

  1. Open ~/.bash_profile by running vim ~/.bash_profile通过运行vim ~/.bash_profile打开~/.bash_profile /.bash_profile
  2. Open ~/.zshrc by running vim ~/.zshrc通过运行vim ~/.zshrc打开~/.zshrc /.zshrc
  3. Copy the content of ~/.bash_profile into ~/.zshrc~/.bash_profile的内容复制到~/.zshrc

Open a new terminal window and run your previous aliases/scripts, which should work flawlessly.打开一个新的终端窗口并运行您以前的别名/脚本,它应该可以完美运行。

Other simple alternative to continue using your .bash_profile is add this file to your .zshrc file:继续使用 .bash_profile 的其他简单替代方法是将此文件添加到 .zshrc 文件中:

  1. Open your .zhsrc file > vim ~/.zshrc打开你的 .zhsrc 文件 > vim ~/.zshrc
  2. Add this line to your .zshrc file > source ~/.bash_profile将此行添加到您的 .zshrc 文件 > source ~/.bash_profile

with this simple solution you can continue adding your .bash_prifile if you like zhs.使用这个简单的解决方案,如果您喜欢 zhs,您可以继续添加您的 .bash_prifile。

Adding .bash_profile添加 .bash_profile

There are five separate profile scripts that get executed (in the order given below) when we launch a zsh shell or close it out.当我们启动 zsh shell 或将其关闭时,会执行五个单独的配置文件脚本(按下面给出的顺序)。

(1) .zshenv --> This is always sourced first but can be overridden by other (1) .zshenv --> 这始终是第一个来源,但可以被其他文件覆盖

(2).zprofile --> This is equivalent for users coming from ksh experience (2).zprofile --> 这对于来自 ksh 经验的用户来说是等价的

(3).zshrc --> This is for all of the interactive customizations of zsh (3).zshrc --> 这用于 zsh 的所有交互式自定义

(4).zlogin --> This executes after first three are done (4).zlogin --> 前三个完成后执行

(5).zlogout --> This is executed when we logout of the zsh shell it would be advisable to put your stuff in .zshenv or in .zshrc (5).zlogout --> 这是在我们注销 zsh shell 时执行的,建议将你的东西放在 .zshenv 或 .zshrc 中

It is not mandatory to have any one of these files.拥有这些文件中的任何一个都不是强制性的。 But if it is there, it will be sourced from and executed in the above order.但如果它在那里,它将按上述顺序获取并执行。

In Mac Catalina onwards osx versions, the terminal uses zsh.在 Mac Catalina 及以上的 osx 版本中,终端使用 zsh。 There is a system-wide profile /etc/zprofile.有一个系统范围的配置文件 /etc/zprofile。

cat /etc/zprofile猫 /etc/zprofile

# System-wide profile for interactive zsh(1) login shells.

# Setup user specific overrides for this in ~/.zprofile. See zshbuiltins(1)
# and zshoptions(1) for more details.

if [ -x /usr/libexec/path_helper ]; then
    eval `/usr/libexec/path_helper -s`
fi

it says , if you want to override then create ~/.zprofile.它说,如果你想覆盖然后创建 ~/.zprofile。

touch ~/.zprofile.

In Mac Catalina, terminal uses zsh.在 Mac Catalina 中,终端使用 zsh。 Instead of having .bash_profile, good to have .zshenv and write your script there.与其拥有 .bash_profile,不如拥有 .zshenv 并在那里编写脚本。

When you open terminal next every time, scripts inside .zshenv gets executed.每次下次打开终端时,都会执行 .zshenv 中的脚本。

I was running into this issue and I followed Zack and Luke Schoen's answer, but my $PATH didn't look the same as what I had in bash .我遇到了这个问题,我遵循了 Zack 和 Luke Schoen 的回答,但我的$PATH看起来与我在bash中的不同。

This post explains what the different config files do: https://unix.stackexchange.com/questions/71253/what-should-shouldnt-go-in-zshenv-zshrc-zlogin-zprofile-zlogout这篇文章解释了不同配置文件的作用: https ://unix.stackexchange.com/questions/71253/what-should-shouldnt-go-in-zshenv-zshrc-zlogin-zprofile-zlogout

I found that splitting my .bash_profile path exports into .zprofile and my aliases into .zshrc worked best for what I wanted.我发现将我的.bash_profile路径导出拆分为.zprofile并将我的别名拆分为.zshrc最适合我想要的。

I found why Zack and Luke Schoen's answer didn't work for me:我发现为什么 Zack 和 Luke Schoen 的回答对我不起作用:

The path exports that I listed in .zshenv were executed first and /usr/libexec/path_helper was executed afterwards, which prepended the paths listed in /etc/paths .我在.zshenv中列出的路径导出首先被执行,然后/usr/libexec/path_helper被执行,这在/etc/paths中列出的路径之前。

I found the profile file under /etc/zprofile location.我在/etc/zprofile位置下找到了配置文件。 This will be for zsh这将用于zsh

update: macOS Monterey 12.4更新:macOS 蒙特雷 12.4

yes - for Zsh, it is the file: .zshrc add there your parameter.是的 - 对于 Zsh,它是文件: .zshrc添加你的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM