~/.profile, ~/.bashrc, and ~/.bash_profile not running on new terminal start up
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.
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.
Related videos on Youtube
DazedAndConfused
Updated on September 10, 2021Comments
-
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.
-
ptierno almost 10 yearswhat terminal are you using? iTerm?
-
Ricardo over 3 yearsUseful to understand why: Why doesn't .bashrc run automatically? and What is the difference between .bash_profile and .bashrc?
-
-
DazedAndConfused almost 10 yearsYay! This fixed it! Thanks for the help!
-
Meghana Randad over 5 yearsThanks your idea helped me
-
Jeremy over 4 yearsSee this post for details for setting up nvm on zsh: stackoverflow.com/a/47017363/230119
-
thenomadicmann over 3 yearsHow do I undo this? That is working for me and now my terminal text is all messed up.
-
Islam Attrash about 3 yearsWorked for me! Thanks
-
tripleee almost 3 yearsThis is deeply flawed. You should never have one shell shource the startup files of another.
-
tripleee almost 3 years@thenomadicmann You should edit the
.zshrc
file and remove thesource
statement you added as the very last line. -
tripleee almost 3 yearsThis 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
tosource ~/.profile
which is designed for this.) -
tripleee almost 3 yearsNone 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 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 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 almost 3 yearsAgain, the only possible situation where
sudo
will be necessary and useful is if you incorrectly usedsudo
before. You should never usesudo
to manipulate your own files because that will cause them to be owned byroot
instead of yourself, which is basically a system integrity error.