.profile 和 .bash_profile 有什么区别,什么时候配置哪个?(苹果电脑)

Imr*_*ran 25 linux mac unix terminal macos

可能的重复:
.bashrc 和 .bash_profile 之间的区别

.profile和之间有什么区别,什么.bash_profile时候配置哪个?

即如果我配置了.bash_profile,我还需要配置.profile吗?

小智 15

假设bash是您的默认 shell,bash手册页 ( man bash)中描述了差异:

   When bash is invoked as an interactive login shell, or as  a  non-interac-
   tive  shell  with the --login option, it first reads and executes commands
   from the file /etc/profile, if that file exists.  After reading that file,
   it  looks  for  ~/.bash_profile,  ~/.bash_login,  and  ~/.profile, in that
   order, and reads and executes commands from the first one that exists  and
   is readable.  The --noprofile option may be used when the shell is started
   to inhibit this behavior.
Run Code Online (Sandbox Code Playgroud)

手册页中有更多详细介绍 POSIX-shell 兼容模式,这是.profile从哪里来的。阅读整个手册页部分以了解所有详细信息。


spe*_*mac 5

如果您配置.bash_profile,则您也不需要配置.profile

我喜欢保留自己的别名和命令,.profile这样如果我弄乱了任何东西,我知道我可以随时删除它们,.profile而不会影响整个系统或其他修改的应用程序.bash_profile(如 MacPorts)。

要使用.profile,您可能需要包括线路source ~/.profile.bash_profile使得.profile文件读取(参见斯内德Deily的答案)。


小智 2

当您获得正常的 shell 进程时,例如您打开终端工具,.profile 将由 bash 执行。.bash_profile 由登录 shell 的 bash 执行——例如,当您远程登录/ssh 进入您的计算机时。例如,如果您远程 ssh 到一台机器(假设您打开一个 X 终端),您将首先执行 .bash_profile。如果在该 XTerminal 中输入“xterm”并生成另一个 X Terminal,则将为 XTerminal 的第二个实例执行 .profile。这些文件位于您的主目录 (~) 中。如果我没有弄错的话,默认情况下它们都会执行 ~/.bashrc ,这样你就可以编辑它来配置登录和非登录 shell 的通用设置/变量(例如设置 PATH、一些别名/快捷方式等)。您可能偶尔希望为这两个设备进行不同的某些配置,但我从未遇到过这种需要——但是,可能性是存在的。