Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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?

like image 669
Blaszard Avatar asked Apr 23 '14 02:04

Blaszard


People also ask

Is bash_profile used for zsh?

bash_profile , in that both are only sourced by their respective shells for login shells. . zshenv is executed for all instances of zsh , whether or not they are login shells. Agreed; . bash_profile has to do for bash what .

Can I use bash commands in zsh?

A ZSH shell script is a text file that contains instructions or commands to be executed by the ZSH shell. The ZSH shell is an extended version of the Bourne Again Shell; thus, most commands and scripts written for bash will work on ZSH.

How do I change from zsh to bash on Mac?

From System Preferences Hold the Ctrl key, click your user account's name in the left pane, and select “Advanced Options.” Click the “Login Shell” dropdown box and select “/bin/bash” to use Bash as your default shell or “/bin/zsh” to use Zsh as your default shell. Click “OK” to save your changes.

How do I access my zsh profile?

The zsh shell provides the . zprofile under the user home directory in order to load profile related configuration. The path is ~/. zprofile and can be used like a bash profile file with zsh commands.


1 Answers

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.


Similarly, you could also move the common profile settings to separate file, i.e. .my_common_profile, and add the following to both .bash_profile and .zshrc:

if [ -f ~/.my_common_profile ]; then      . ~/.my_common_profile; fi 
like image 93
Yuchen Avatar answered Sep 20 '22 09:09

Yuchen