Best practice for Bash start-up files on a Mac
As I understand it, the order that start-up files 开发者_StackOverflow中文版are read by the bash shell on a Mac are...
- ~/.bash_profile
- ~/.bash_login
- ~/.profile
..and once one file in this list is found, the contents of the other is ignored.
That being said, is there a best practice for which of these files should be my one true Bash start-up file?
On one hand, if .bash_profile will take precedence over any other potential start-up file, then that should be used, because you can be sure that 100% of the time the info in that start-up file is being run.
On the other hand, if .profile is the file that exists on Mac systems by default, and .bash_profile needs to be manually created, then perhaps that should be used, and there will never be a reason to create a .bash_profile file.
Thoughts?
It depends on whether you use shells other than bash, and whether you use bash-only features in your profile. If you use other sh-style shells (sh, ksh, zsh, etc but not csh or tcsh), don't use bash-only features and want the same setup no matter what shell you're in, you should use .profile. If you want to use bash-only features, use .bash_profile. If you want to use multiple shells but also use bash-only features, put the common stuff in .profile and the bash-only stuff in .bash_profile, then add if [ -f ~/.profile ]; then . ~/.profile; fi
to .bash_profile.
If you only ever use bash, but don't rely on any bash-only features in your profile, then it doesn't really matter.
There's actually another complication: login bash shells source either .bash_profile, .bash_login, or .profile; non-login interactive bash shells (e.g. subshells) source .bashrc instead. I tend to want the same setup in both login and non-login shells, so I put all the interesting stuff in .bashrc, and then if [ -f ~/.bashrc ]; then . ~/.bashrc; fi
in .bash_profile. If I also used other shells, I'd probably put most of it in .profile instead, and have .bashrc source that instead.
Just in case, I had a problem before where I had a lost configuration somewhere and took me a long time to find it, mainly because I was a noob.
I was looking into these user specific files these users very well determined, but the configuration was set on /etc/profile.
Just in case.
精彩评论