~/.profile, ~/.bashrc, and ~/.bash_profile not running on new terminal start up

41,210

Solution 1

Two things need to happen here when using iTerm to get the loading of dotfiles to work.

First you should add the following to your .bash_profile

[[ -s ~/.bashrc ]] && source ~/.bashrc

Secondly you will need to ensure that in iTerm preferences your terminal is set to launch a login shell.

iTerm Preferences

Hope this helps!

Solution 2

Newer MacOS versions use zsh as the default shell for both Terminal and iTerm2. Run echo $SHELL to confirm if this is the case for you.

Zsh looks for a .zshrc file upon shell startup, so to continue using zsh while sourcing the contents of your bash profile, you can run the following:

echo "source ~/.bash_profile" >> ~/.zshrc

Open a new terminal window for the changes to take effect.

Solution 3

Using the default mac terminal, what worked for me was to add a command to run on start up to source my .bash_profile.

Preferences > Profile > Startup > Add command 'source ~/.bash_profile'

Mac terminal preferences window screenshot

Might be considered to be a bit hacky, but it does the trick.

Solution 4

Adding source ~/.profile to my .bash_profile worked for me.

Solution 5

As of High Sierra, both Terminal and iTerm want to load ~/.profile first. So I suggest you put one line in your .profile to make your Mac work like other Unixes:

source ~/.bash_profile

By editing this one file, you won't have to search through the menus of multiple apps to override Apple's bizarre behavior in each.

Share:
41,210

Related videos on Youtube

DazedAndConfused
Author by

DazedAndConfused

Updated on September 10, 2021

Comments

  • DazedAndConfused
    DazedAndConfused almost 3 years

    I am trying to create a permanent alias for my terminal. I put the alias in my ~/.profile, ~/.bashrc, and ~/.bash_profile files, previously empty. When I start a new terminal, bash does not recognize the alias, but if I source any of them, it does. Why are these not getting run when I open a terminal? I am on OSX.

  • DazedAndConfused
    DazedAndConfused almost 10 years
    Yay! This fixed it! Thanks for the help!
  • Meghana Randad
    Meghana Randad over 5 years
    Thanks your idea helped me
  • Jeremy
    Jeremy over 4 years
    See this post for details for setting up nvm on zsh: stackoverflow.com/a/47017363/230119
  • thenomadicmann
    thenomadicmann over 3 years
    How do I undo this? That is working for me and now my terminal text is all messed up.
  • Islam Attrash
    Islam Attrash about 3 years
    Worked for me! Thanks
  • tripleee
    tripleee almost 3 years
    This is deeply flawed. You should never have one shell shource the startup files of another.
  • tripleee
    tripleee almost 3 years
    @thenomadicmann You should edit the .zshrc file and remove the source statement you added as the very last line.
  • tripleee
    tripleee almost 3 years
    This is incorrect, because it will cause non-Bash shells to attempt to source a file which is reserved for Bash and may contain syntax which is not compatible with other shells. (Doing the reverse is harmless; edit .bash_profile to source ~/.profile which is designed for this.)
  • tripleee
    tripleee almost 3 years
    None of this nonsense will be necessary if you didn't incorrectly use sudo to manipulate your personal files in the first place. This is a common beginner error as such, but still not a good general solution for other situations.
  • lchapo
    lchapo almost 3 years
    @tripleee to make your feedback more constructive, I suggest identifying some of the risks with this approach and suggesting an alternative
  • tripleee
    tripleee almost 3 years
    @lchapo Several answers here identify other approaches. I don't think explaining the risks in more detail is really necessary here, and I'm repeating things I have said in other comrents here; but trivially, using Bash-only syntax in this file will break it for any shell which doesn't support that syntax. (Zsh can be configured to understand Bash syntax, but this locks you into requiring this configuration option, which then raises doubts about the rationale of preferring Zsh over Bash.)
  • tripleee
    tripleee almost 3 years
    Again, the only possible situation where sudo will be necessary and useful is if you incorrectly used sudo before. You should never use sudo to manipulate your own files because that will cause them to be owned by root instead of yourself, which is basically a system integrity error.