2

I am on a MAC running Yosemite.

Until recently, I only had a .profile, in which I added directories to my PATH.

Now I have a .bash_profile, which from what I experienced and later read online, causes the .profile to be ignored when opening a terminal window.

After some readings, I do understand how .pofile and .bash_profile differ. I do get that .bash_profile is only invoked when starting a shell.

I am however still not sure, for a basic user, what the drawbacks are, if any, in either:

  • calling .profile from .bash_profile,
  • or simply ignoring .bash_profile and setting all environment variables I may need from a shell in the .profile file directly.

For example, I read here that since .profile is ignored when .bash_profile is present, I could just add

. ~/.profile 

in my bash_profile to invoke .profile.

And alternatively, though I understand that .bash_profile would allow me to set variables not needed outside of bash, I still can set everything I need in .profile and do not see what using .bash_profile brings me.

marked as duplicate by jasonwryan, Anthon, jordanm, Michael Homer, slm Mar 26 '15 at 5:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    I read about this post (and others). My question is about recommended usage here. – Lolo Mar 25 '15 at 21:58
  • See also: unix.stackexchange.com/questions/45684/… The answer as to what is recommended is pretty much "what works for you"... – jasonwryan Mar 25 '15 at 22:01
4

Traditional sh reads the file ~/.profile at startup when it's a login shell.

Bash, which is backwards compatible with sh but offers additional features, reads .bash_profile, and if that doesn't exist, tries .profile instead¹. This allows you to have a .profile for plain sh and a .bash_profile taking advantage of bash's extra features when your login shell is bash.

Bash also reads another file, .bashrc, when it's invoked interactively. That's the place to put prompts, aliases, completion setup, etc. However, it doesn't read that file in an interactive login shell. So your .bash_profile should source .bashrc when the shell is running interactively — otherwise you'd have to maintain separate files.

There's rarely any reason to depart from the following content for .bash_profile:

. ~/.profile
case $- in *i*) . ~/.bashrc;; esac

Put login-time things like environment variable definitions in .profile and things for interactive shells in .bashrc.

If you only ever use bash as your login shell, you might instead not have a .profile, and put login-time stuff directly in .bash_profile, plus the line to load .bashrc when interactive.

¹ It also tries .bash_login before .profile but that's not really useful.

  • Thank you for actually answering my question regarding recommended usage! – Lolo Mar 26 '15 at 20:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.