我一直在研究 bash 的工作原理,到目前为止我已经了解了以下内容:
当启动登录 shell 时,将执行下列存在的第一个文件:
~/.bash_profile
, ~/.bash_login
,~/.profile
当启动交互式的非登录 shell(或子 shell)时,该~/.bashrc
文件将被执行。
此外,.profile 还可以由 sh 等其他 shell 执行。我现在的问题是,拥有 .bash_profile 和 .bash_login 有什么意义?它们都执行相同的功能,并且与 .profile 不同,.bash_profile 和 .bash_login 都只能由 bash 读取。我知道它们之间的唯一区别是,如果 .bash_profile 不存在,则 .bash_login 会被执行。那么为什么它会在那里呢?
答案1
shellcsh
从中bash
获得了一些功能,用作.login
启动登录 shell 时运行的 shell 启动文件的名称,就像ksh
另一个对 shell 影响很大的 shell一样bash
,用作.profile
登录 shell 的启动文件。
因此,从shell 的文件.bash_profile
中借用其名称,同时从shell 的文件中借用其名称。ksh
.profile
.bash_login
csh
.login
用户使用.bash_profile
或.bash_login
依赖于他们要迁移到的其他 shell 系列(ksh
类似 shell 或csh
类似 shell)bash
。
显然,如今,许多 Linux 用户从未使用过除 之外的其他 shell bash
,因此他们使用的任何文件名很可能取决于系统管理员、老师的偏好,或者可能是随机的。
如果 和 都~/.bash_profile
存在~/.bash_login
,则该~/.bash_login
文件将被忽略。
答案2
使用可选脚本名称的优点之一~/.bash_login
,而不是~/.profile
,您的注销脚本是否有一个并行名称,~/.bash_logout
,如果您决定使用它。
因此,如果您运行的是 Bash,而不是 Bourne,这会简化配置名称集的复杂性:
/etc/profile # system login script
~/.bash_profile # user login script
~/.bash_logout # user logout script
/etc/bash.bashrc # system interactive script
~/.bashrc # user interactive script
对此:
/etc/profile # system login script
~/.bash_login # user login script <--sisters +
~/.bash_logout # user logout script <----------+
/etc/bash.bashrc # system interactive script
~/.bashrc # user interactive script
但是,如果您不打算使用注销脚本,那么其他方法对您来说可能更简单:
/etc/profile # system login script
~/.bash_profile # user login script
/etc/bash.bashrc # system interactive script
~/.bashrc # user interactive script
太糟糕了,配置名称中没有更多的名称对称性......
但是您可以使用这些符号链接使这变得更好:
/etc/bash --> /etc/profile # system login script
~/.bash --> ~/.bash_profile # user login script
/etc/bashrc --> /etc/bash.bashrc # system interactive script
~/.bashrc # user interactive script
非破坏性地创建:
sudo ln -sT /etc/profile /etc/bash
ln -sT ~/.bash_profile ~/.bash
sudo ln -sT /etc/bash.bashrc /etc/bashrc
# ~/.bashrc (don't change)
或者您可以更积极地扭转这一点,将文件移动到新名称,然后反向链接它们:
sudo mv -f /etc/profile /etc/bash && \
sudo ln -sT /etc/bash /etc/profile
sudo mv -f /etc/bash.bashrc /etc/bashrc && \
sudo ln -sT /etc/bashrc /etc/bash.bashrc
mv -f ~/.bash_profile ~/.bash && \
ln -sT ~/.bash ~/.bash_profile
# ~/.bashrc (don't change)
不管怎样,现在你有了这些更统一的配置文件名,当您从链接中忘记它们时很容易找到:
/etc/bash # system login config script
~/.bash # user login config script
/etc/bashrc # system interactive config script
~/.bashrc # user interactive config script
(我不使用注销脚本,但它可能会被命名为:/etc/bash.logout
。)