Mac 上 Bash 启动文件的最佳实践
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4493063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best practice for Bash start-up files on a Mac
提问by Bob.
As I understand it, the order that start-up files are read by the bash shell on a Mac are...
据我了解,Mac 上的 bash shell 读取启动文件的顺序是...
- ~/.bash_profile
- ~/.bash_login
- ~/.profile
- ~/.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?
话虽如此,对于这些文件中的哪一个应该是我的一个真正的 Bash 启动文件,是否有最佳实践?
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.
一方面,如果 .bash_profile 优先于任何其他潜在的启动文件,那么应该使用它,因为您可以确保 100% 的时间该启动文件中的信息正在运行。
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.
另一方面,如果 .profile 是 Mac 系统默认存在的文件,而 .bash_profile 需要手动创建,那么也许应该使用它,并且永远没有理由创建 .bash_profile 文件。
Thoughts?
想法?
回答by Gordon Davisson
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; fito .bash_profile.
这取决于您是否使用 bash 以外的 shell,以及您是否在个人资料中使用 bash-only 功能。如果您使用其他 sh 样式的 shell(sh、ksh、zsh 等,但不使用 csh 或 tcsh),请不要使用仅限 bash 的功能,并且无论您使用哪种 shell,都需要相同的设置,您应该使用 .profile . 如果您想使用仅限 bash 的功能,请使用 .bash_profile。如果你想使用多个shell但也使用bash-only特性,把常用的东西放在.profile中,把bash-only的东西放在.bash_profile中,然后添加if [ -f ~/.profile ]; then . ~/.profile; fi到.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.
如果您只使用过 bash,但不依赖您的个人资料中的任何 bash-only 功能,那么这并不重要。
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; fiin .bash_profile. If I also used other shells, I'd probably put most of it in .profile instead, and have .bashrc source that instead.
实际上还有另一个复杂性:登录 bash shell 源代码为 .bash_profile、.bash_login 或 .profile;非登录交互式 bash shell(例如 subshell)源 .bashrc 代替。我倾向于在登录和非登录 shell 中使用相同的设置,所以我将所有有趣的东西放在 .bashrc 中,然后if [ -f ~/.bashrc ]; then . ~/.bashrc; fi放在 .bash_profile 中。如果我还使用了其他 shell,我可能会将大部分内容放在 .profile 中,而使用 .bashrc 源代码。
回答by bitoiu
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.
我正在查看这些用户特定的文件,这些用户非常确定,但配置是在 /etc/profile.d 上设置的。
Just in case.
以防万一。

