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

浪尽此生 提交于 2019-12-02 15:10:04

An interactive bash reads your ~/.bash_profile if it's a login shell, or your ~/.bashrc if it's not a login shell.

A typical .bash_profile will contain something like:

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

so .bashrc can contain commands to be executed by either login or non-login shells.

If you run bash -l rather than just bash, it should read your .bash_profile.

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

Open ~/.zshrc, and at the very bottom of the file, add the following:

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). With that, you can keep your custom settings for zsh (colors, and etc). And you get to keep your custom shell settings in .bash_profile file.

This is much cleaner than using bash -l IMO.

If you prefer putting your settings in .bashrc, or .bash_login, or .profile, you can do the same for them.

To complement @Keith Thompson's excellent answer:

macOS:

As @chepner puts it succinctly (emphasis mine):

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.

As a result, some OSX users only ever create ~/.bash_profile, and never bother with ~/.bashrc, because ALL interactive bash shells are login shells.

Linux:

On Linux, the situation is typically reversed: bash shells created interactively are [interactive] NON-login shells, so it is ~/.bashrc that matters.

As a result, many Linux users only ever deal with ~/.bashrc.


To maintain bash profiles that work on BOTH platforms, use the technique @Keith Thompson mentions:

  • Put your definitions (aliases, functions, ...) in ~/.bashrc
  • Add the following line to ~/.bash_profile
[[ -f ~/.bashrc ]] && . ~/.bashrc
Sandeep Kumar

Copy the contents from ~/.bash_profile and paste them at the bottom of ~/.zshrc file.

For ZSH users on MacOs, I ended up with a one liner.

At the very bottom of the ~/.zshrc I added the following line :

bash -l

What it does is simply load the .bash_profile settings(aliases, function, export $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.

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_profile

This will pull in the settings you make in the .bash_profile script for the life of that shell session. 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.

For those who have just installed zsh and want their alias from bash to work on zsh do the following

  1. Open .zshrc file in vim like so

     vi ~/.zshrc
    
  2. Scroll to the bottom

  3. click "i" to enable write mode
  4. Tell zsh to load items from bash_profile when needed like so
    source ~/.bash_profile
    
  5. Write and quit like so
    :wq
    
  6. Refresh your zsh like so
    source ~/.zshrc
    
    That's it. Now all your saved alias in .bash_profile will be ready to use in zsh.

Recently I installed oh-my-zsh on OS X and set zsh as default shell and faced the same problem.
I solved this problem by adding source ~/.bash_profile at the end of .zshrc file.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!