How to load ~/.bash_profile when entering bash from within zsh?
Solution 1
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
Solution 2
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
Solution 3
For those who have just installed zsh and want their alias from bash to work on zsh do the following
-
Open .zshrc file in vim like so
vi ~/.zshrc
Scroll to the bottom
- click "i" to enable write mode
- Tell zsh to load items from bash_profile when needed like so
source ~/.bash_profile
- Write and quit like so
:wq
- Refresh your zsh like so
That's it. Now all your saved alias in .bash_profile will be ready to use in zsh.source ~/.zshrc
Solution 4
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
Solution 5
Copy the contents from ~/.bash_profile and paste them at the bottom of ~/.zshrc file.
Related videos on Youtube
Blaszard
I'm here to gain knowledge and insights on a variety of fields I'm interested in. Specifically, Programming & Software Development (Python and R; no longer use Swift and JavaScript/node.js) Data Science, Machine Learning, AI, & statistics Travel (started in 2016) Language (普通话, français, español, italiano, русский, 한국어) Politics, Economics, and Finance Currently (in 2020), my primary interest is Korean and Russian😈 PS: I'm not a native-English speaker. If you find any errors in my grammar and expressions, don't hesitate to edit it. I'll appreciate it👨🏻💼
Updated on January 25, 2022Comments
-
Blaszard over 2 years
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
likealias julia="~/juila/julia"
, etc.. Also, the prompt is not what I set in the file and instead returnbash-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?-
Will about 10 yearsare your aliases available after your run
source ~/.bash_profile
?
-
-
Blaszard about 10 yearsThanks. I defined the
~/.bashrc
loading on the top of my~/.bash_profile
, but defined almost all settings, including aliases and environmental variables in my~/.bash_profile
, not~/.bashrc
. And I confirm thatbash -l
loads it successfully from within zsh. -
Blaszard about 10 yearsThanks for the excellent follow-up! How do you think about the idea that I only leave
if [ -f ~/.bashrc ]; then . ~/.bashrc; fi
in my~/.bash_profile
, and move everything others to~/.bashrc
? -
mklement0 about 10 years@Gardecolo: I think that's a good idea (there's a largely hypothetical caveat: on platforms where a
bash
login session is created during boot - as stated, OSX is NOT one of them - you could argue that allexport
statements should go into~/.bash_profile
, because they only need to be executed once, and repeating them for every new bash shell is unnecessary - in practice, though, I doubt that it matters). -
gsscoder over 8 yearsI don't know why oh-my-zsh updates, it removes every change to .zshrc.
-
skiabox about 7 yearsThis works but it creates another bash shell inside zsh and you lose zsh functionality and colors.
-
noun over 6 yearsInstead of this, at the very bottom of
~/.zshrc
file, addsource ~/.bash_profile
.