20

I installed some command line programs (e.g. rbenv), but every time I open a new Terminal tab, it looks like the environment gets reset. I need to source my .bash_profile manually:

source ~/.bash_profile

I followed some tutorials and put this code in my .bash_profile, but it doesn't work:

if [ -n "$BASH_VERSION" ] && [ -f $HOME/.bashrc ];then
    source $HOME/.bashrc
fi

This doesn't happen on my old Mac, and I don't know how to configure this.

Allan
  • 101,432
hqt
  • 369
  • What do you mean by the code "doesn't work"? - How doesn't it work? - Do you actually have a .bashrc file? – jksoegaard Feb 20 '18 at 14:59
  • Have you tried closing Terminal.app and starting it over? – jksoegaard Feb 20 '18 at 15:00
  • yes. i have created bashrc file. (and run source ~/.bash_profile) – hqt Feb 20 '18 at 15:01
  • @jksoegaard yes. I have restarted terminal app. – hqt Feb 20 '18 at 15:01
  • And what is your problem exactly? - That the code inside .bash_profile is not run? - or that the code inside your .bashrc file is not run? Are you sure you named it ".bashrc" and not "bashrc" (note the leading dot) – jksoegaard Feb 20 '18 at 15:32
  • If .bash_profile doesn't get sourced automatically on startup, changing its content (as you describe in the question) won't make it get sourced. What is in your .bashrc, in your .bash_profile, in your .profile? – nohillside Feb 20 '18 at 15:51

3 Answers3

32

If you are using oh-my-zsh, the default one that will be loaded automatically is ~/.zshrc. All you need to do is adding the following at the end of ~/.zshrc:

if [ -f ~/.bash_profile ]; then
  . ~/.bash_profile
fi
Yuchen
  • 421
7

.bashrc will be loaded per default, so if you have all your stuff in .bash_profile ensure it will be loaded, so you have to add to your .bashrc:

if [ -f ~/.bash_profile ]; then
  . ~/.bash_profile
fi
LeFnord
  • 71
4

You can edit your environment in ~/.zprofile instead of ~/.bash_profile. Then, reopen a terminal.

  • On MacOS Monterey this was the only answer that worked for me (by running .bashrc inside .zprofile). .bashrc, .bash_profile, and .zshrc are not run on their own. – Joshua M Nov 04 '22 at 17:57
  • works for me fine with .zprofile – sami Oct 04 '23 at 22:02