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

11

103

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?

Rask answered 23/4, 2014 at 2:11 Comment(1)
are your aliases available after your run source ~/.bash_profile?Prod
S
45

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

Stinkstone answered 23/4, 2014 at 2:17 Comment(2)
Thanks. 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 that bash -l loads it successfully from within zsh.Rask
I don't know why oh-my-zsh updates, it removes every change to .zshrc.Smart
T
174

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
Thumbtack answered 20/1, 2018 at 19:27 Comment(4)
what does '-f ~/.bash_profile' and '. ~/.bash_profile' mean? A shell command??Anyway
The 1st one means "does this file exist?". The 2nd one means "load this file please".Thumbtack
@Anyway this might help https://mcmap.net/q/36097/-how-do-i-tell-if-a-file-does-not-exist-in-bash/1035008Thumbtack
Thanks!!! I take this very comprehensive documentation after you. tldp.org/LDP/Bash-Beginners-Guide/html/index.htmlAnyway
S
45

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

Stinkstone answered 23/4, 2014 at 2:17 Comment(2)
Thanks. 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 that bash -l loads it successfully from within zsh.Rask
I don't know why oh-my-zsh updates, it removes every change to .zshrc.Smart
R
39

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.
Rossiter answered 28/8, 2019 at 1:44 Comment(0)
S
28

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
Sheri answered 23/4, 2014 at 2:55 Comment(2)
Thanks 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?Rask
@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 all export 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).Sheri
F
20

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

Frowst answered 3/11, 2015 at 22:50 Comment(0)
W
8

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.

Wharton answered 14/11, 2016 at 9:21 Comment(2)
This works but it creates another bash shell inside zsh and you lose zsh functionality and colors.Direction
Instead of this, at the very bottom of ~/.zshrc file, add source ~/.bash_profile .Salmonoid
E
4

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.

Endodontics answered 23/4, 2014 at 17:57 Comment(0)
C
4

For macOS Big Sur (Version 11.5.2)

  • Open .zshrc

    • For example: sudo nano ~/.zshrc
  • At the end of the file add source ~/.bash_profile

Every time you open the terminal the contents inside the bash profile will be loaded.

Cinquain answered 22/9, 2021 at 9:41 Comment(0)
C
2

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.

Cleland answered 13/9, 2018 at 21:8 Comment(0)
S
1

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. However, these steps worked for me.

  1. Add new alias(es) at the bottom of my .bash_profile

    vi ~/.bash_profile

  2. Make zsh to load items from .bash_profile

    source ~/.bash_profile

  3. Refresh zsh

    source ~/.zshrc

  4. Restart OSX Terminal app

  5. Try your new alias!

Sneed answered 27/4, 2020 at 2:31 Comment(0)
H
1

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

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

.bash_profile and .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.

Headsman answered 11/6, 2021 at 16:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.