简体   繁体   English

从zsh中输入bash时如何加载〜/ .bash_profile?

[英]How to load ~/.bash_profile when entering bash from within zsh?

I've used bash for two years, and just tried to switch to zsh shell on my OS X via homebrew.我已经使用 bash 两年了,只是尝试通过自制软件在我的 OS X 上切换到 zsh shell。 And I set my default (login) shell to zsh, and I confirmed it's set properly by seeing that when I launch my Terminal, it's zsh shell that is used in default.我将我的默认(登录)shell 设置为 zsh,并确认它设置正确,因为当我启动终端时,它是默认使用的 zsh shell。

However, when I try to enter bash shell from within zsh, it looks like not loading ~\/.bash_profile<\/code> , since I cannot run my command using aliases, which is defined in my ~\/.bash_profile<\/code> like alias julia="~\/juila\/julia"<\/code> , etc.. Also, the prompt is not what I set in the file and instead return bash-3.2$<\/code> .但是,当我尝试从 zsh 中进入 bash shell 时,它似乎没有加载~\/.bash_profile<\/code> ,因为我无法使用别名运行我的命令,别名是在我的~\/.bash_profile<\/code>定义的,例如alias julia="~\/juila\/julia"<\/code>等。此外,提示不是我在文件中设置的,而是返回bash-3.2$<\/code> 。

For some reasons, when I set my login shell to bash, and enter zsh from within bash, then ~\/.zshrc<\/code> is loaded properly.由于某些原因,当我将登录 shell 设置为 bash 并从 bash 中输入 zsh 时, ~\/.zshrc<\/code>会正确加载。

So why is it not loaded whenever I run bash<\/code> from within zsh?那么为什么当我从 zsh 中运行bash<\/code>时它没有加载呢? My ~\/.bash_profile<\/code> is symbolic linked to ~\/Dropbox\/.bash_profile<\/code> in order to sync it with my other computers.我的~\/.bash_profile<\/code>符号链接到~\/Dropbox\/.bash_profile<\/code>以便与我的其他计算机同步。 Maybe does it cause the issue?也许它会导致问题?

"

Open ~/.zshrc , and at the very bottom of the file, add the following:打开~/.zshrc ,并在文件的最底部,添加以下内容:

if [ -f ~/.bash_profile ]; then 
    . ~/.bash_profile;
fi

Every time you open the terminal, it will load whatever is defined in ~/.bash_profile (if the file exist).每次打开终端时,它都会加载~/.bash_profile定义的任何内容(如果文件存在)。 With that, you can keep your custom settings for zsh (colors, and etc).这样,您可以保留zsh的自定义设置(颜色等)。 And you get to keep your custom shell settings in .bash_profile file.您可以将自定义 shell 设置保存在.bash_profile文件中。

This is much cleaner than using bash -l IMO.这比使用bash -l IMO 干净得多。

If you prefer putting your settings in .bashrc , or .bash_login , or .profile , you can do the same for them.如果您更喜欢将设置放在.bashrc.bash_login.profile ,您可以对它们执行相同的操作。

An interactive bash reads your ~/.bash_profile if it's a login shell, or your ~/.bashrc if it's not a login shell.交互式bash读取您的~/.bash_profile如果它是登录 shell,或者您的~/.bashrc如果它不是登录 shell。

A typical .bash_profile will contain something like:典型的.bash_profile将包含以下内容:

if [ -f ~/.bashrc ]; then . ~/.bashrc; fi

so .bashrc can contain commands to be executed by either login or non-login shells.因此.bashrc可以包含要由登录或非登录 shell 执行的命令。

If you run bash -l rather than just bash , it should read your .bash_profile .如果您运行bash -l而不仅仅是bash ,它应该读取您的.bash_profile

Reference: https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html参考: https : //www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html

For those who have just installed zsh and want their alias from bash to work on zsh do the following对于那些刚刚安装 zsh 并希望他们的 bash 别名在 zsh 上工作的人,请执行以下操作

  1. Open .zshrc file in vim like so像这样在vim中打开.zshrc文件

     vi ~/.zshrc
  2. Scroll to the bottom滚动到底部

  3. click "i" to enable write mode单击“i”启用写入模式
  4. Tell zsh to load items from bash_profile when needed like so告诉 zsh 在需要时从 bash_profile 加载项目,就像这样
    source ~/.bash_profile
  5. Write and quit like so像这样写然后退出
    :wq
  6. Refresh your zsh like so像这样刷新你的 zsh
     source ~/.zshrc
    That's it.就是这样。 Now all your saved alias in .bash_profile will be ready to use in zsh.现在,您在 .bash_profile 中保存的所有别名都可以在 zsh 中使用了。

To complement @Keith Thompson's excellent answer:为了补充@Keith Thompson 的出色回答:

macOS :系统

As @chepner puts it succinctly (emphasis mine):正如@chepner 简洁地说的那样(强调我的):

In OS X, bash is not used as part of the initial [at boot time] login process, and the Terminal.app (or other terminal emulators) process exists outside any pre-existing bash sessions, so each new window [or tab - read: interactive bash shell] (by default) treats itself as a new login session .在 OS X 中,bash 不用作初始 [在启动时] 登录过程的一部分,并且 Terminal.app(或其他终端模拟器)进程存在于任何预先存在的 bash 会话之外,因此每个新窗口 [或选项卡 -阅读:交互式 bash shell](默认情况下)将自己视为新的登录会话

As a result, some OSX users only ever create ~/.bash_profile , and never bother with ~/.bashrc , because ALL interactive bash shells are login shells.因此,一些 OSX 用户只创建~/.bash_profile ,而从不打扰~/.bashrc ,因为所有交互式 bash shell 都是登录shell。

Linux : Linux

On Linux, the situation is typically reversed : bash shells created interactively are [interactive] NON-login shells, so it is ~/.bashrc that matters.在 Linux 上,情况通常是相反的:交互创建的bash shell 是 [交互式]非登录shell,所以重要的是~/.bashrc

As a result, many Linux users only ever deal with ~/.bashrc .因此,许多 Linux 用户只处理~/.bashrc


To maintain bash profiles that work on BOTH platforms , use the technique @Keith Thompson mentions:要维护适用于两个平台的 bash 配置文件,请使用@Keith Thompson 提到的技术:

  • Put your definitions (aliases, functions, ...) in ~/.bashrc将您的定义(别名、函数等)放入~/.bashrc
  • Add the following line to ~/.bash_profile ~/.bash_profile下行添加到~/.bash_profile
[[ -f ~/.bashrc ]] && . ~/.bashrc

从 ~/.bash_profile 复制内容并将它们粘贴到 ~/.zshrc 文件的底部。

For ZSH users on MacOs, I ended up with a one liner.对于 MacOs 上的 ZSH 用户,我最终得到了一个衬垫。

At the very bottom of the ~/.zshrc I added the following line :~/.zshrc 的最底部,我添加了以下行:

bash -l

What it does is simply load the .bash_profile settings(aliases, function, export $PATH, ...)它所做的只是加载.bash_profile设置(别名、函数、导出 $PATH、...)

If you decide to get rid of ZSH and go back to plain BASH, you'll be back to normal with no hassle at all.如果您决定摆脱 ZSH 并返回到普通的 BASH,您将完全恢复正常。

If this is something that you do infrequently, or it just isn't appropriate to make changes, you can also 'source' the .bash_profile after launching the child bash shell.如果这是您很少做的事情,或者不适合进行更改,您还可以在启动子 bash shell 后“获取” .bash_profile

. ~/.bash_profile

This will pull in the settings you make in the .bash_profile script for the life of that shell session.这将在该 shell 会话的生命周期中引入您在.bash_profile脚本中所做的设置。 In most cases, you should be able to repeat that command, so it's also an easy way to test any changes that you make without needing to do a full login, as well as bring all of your existing shell sessions up-to-date if you make upgrades to the .bash_profile &/or .bashrc files.在大多数情况下,您应该能够重复该命令,因此这也是一种无需完全登录即可测试您所做的任何更改的简单方法,并且如果出现以下情况,则可以使所有现有的 shell 会话保持最新状态您对.bash_profile和/或.bashrc文件进行升级。

For macOS Big Sur (Version 11.5.2)适用于 macOS Big Sur(版本 11.5.2)

  • Open .zshrc<\/code>打开.zshrc<\/code>

    • For example: sudo nano ~\/.zshrc<\/code>例如: sudo nano ~\/.zshrc<\/code><\/li><\/ul><\/li>
    • At the end of the file add source ~\/.bash_profile<\/code>在文件末尾添加source ~\/.bash_profile<\/code>

      <\/li><\/ul>

      Every time you open the terminal the contents inside the bash profile will be loaded.每次打开终端时,都会加载 bash 配置文件中的内容。

      "

Recently I installed oh-my-zsh on OS X and set zsh as default shell and faced the same problem.最近我在 OS X 上安装了oh-my-zsh并将zsh设置为默认 shell 并面临同样的问题。
I solved this problem by adding source ~/.bash_profile at the end of .zshrc file.通过.zshrc文件末尾添加source ~/.bash_profile 解决了这个问题。

I am using a zsh framework called oh my zsh and I have tried most of the solutions listed here and it broke the format for my custom theme.我正在使用一个名为oh my zsh的 zsh 框架,我已经尝试了此处列出的大多数解决方案,但它破坏了我的自定义主题的格式。 However, these steps worked for me.但是,这些步骤对我有用。

  1. Add new alias(es) at the bottom of my .bash_profile在我的.bash_profile底部添加新别名

    vi ~/.bash_profile

  2. Make zsh to load items from .bash_profilezsh.bash_profile加载项目

    source ~/.bash_profile

  3. Refresh zsh刷新zsh

    source ~/.zshrc

  4. Restart OSX Terminal app重新启动 OSX 终端应用程序

  5. Try your new alias!试试你的新别名!

If you'd like to be "profile-centric", you can create .profile as a single source of truth, then load it from both .bash_profile and .zprofile .如果您想“以个人资料为中心”,您可以创建.profile作为单一事实来源,然后从.bash_profile.zprofile加载它。

.profile 。轮廓

export PATH="/usr/local/opt/python/libexec/bin:$PATH"
# etc., etc.

.bash_profile and .zprofile .bash_profile.zprofile

if [ -f ~/.profile ]; then 
    . ~/.profile;
fi

I found this helped bash scripts find the right PATH , etc., and helped me keep configuration in one place.我发现这有助于 bash 脚本找到正确的PATH等,并帮助我将配置保留在一个地方。

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

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