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

Blaszard picture Blaszard · Apr 23, 2014 · Viewed 48.8k times · Source

I've used bash for two years, and just tried to switch to zsh shell on my OS X via homebrew. 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.

However, when I try to enter bash shell from within zsh, it looks like not loading ~/.bash_profile, since I cannot run my command using aliases, which is defined in my ~/.bash_profile like alias julia="~/juila/julia", etc.. Also, the prompt is not what I set in the file and instead return bash-3.2$.

For some reasons, when I set my login shell to bash, and enter zsh from within bash, then ~/.zshrc is loaded properly.

So why is it not loaded whenever I run bash from within zsh? My ~/.bash_profile is symbolic linked to ~/Dropbox/.bash_profile in order to sync it with my other computers. Maybe does it cause the issue?

Answer

Yuchen Zhong picture Yuchen Zhong · Jan 20, 2018

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.