这三个文件是按这个顺序读取的?
.bash_profile
.profile
.bashrc这不是我第一次打开终端的时候发生的。
我在文件init.log后面的文件中有跟踪语句。请看下面的内容。它是在打开终端后开始的。我已经放置了一个注释,以显示在su命令之后日志拾取的位置。
stephen@debian:~$ cat init.log
reading .bashrc
done reading .bashrc
stephen@debian:~$ su - stephen
Password:
stephen@debian:~$ cat init.log
reading .bashrc
done reading .bashrc
#
# after su
#
reading .bash_profile
reading .profile
reading .bashrc
done reading .bashrc
done reading .profile
done reading .bash_profile
stephen@debian:~$ 因此su登录触发了预期的序列,但是初始登录只读取bashrc。这是不对的。有人能解释一下在什么情况下会发生这种情况吗。我可以修改bashrc和profile文件,以便初始读取包含所有预期的文件,但我更希望找到问题的根源,并在那里修复它。
发布于 2018-04-21 04:18:16
答案是bash将查找这三个文件(在稍微不同的情况下),但通常只执行其中一个文件。
当运行登录shell时(通常当您登录到终端上,或者打开GNOME终端或类似的程序时,或者使用su -时),更具体地说,是一个交互式的登录外壳,然后它将执行系统范围的/etc/profile,在完成之后,它将查找~/.bash_profile、~/.bash_login或~/.profile,并首先执行它找到的。
来自bash手册页:
当bash作为交互式登录shell调用,或者作为带有
--login选项的非交互式shell调用时,如果存在该文件,它首先从文件/etc/profile读取和执行命令。读取该文件后,它将按照该顺序查找~/.bash_profile、~/.bash_login和~/.profile,并从存在和可读的第一个文件中读取和执行命令。当外壳程序开始抑制这种行为时,可以使用--noprofile选项。
当bash作为交互式shell执行时,更具体地说,是一个交互式的非登录shell,那么它将读取~/.bashrc并执行该文件。
来自bash手册页:
当启动非登录shell的交互式shell时,bash从
~/.bashrc读取和执行命令,如果该文件存在的话。使用--norc选项可以抑制这一点。--rcfile文件选项将强制bash从文件中读取和执行命令,而不是~/.bashrc。
Linux发行版通常所做的是提供相互链接的~/.bash_profile、~/.profile和~/.bashrc文件,这样您就有了更一致的行为,而不必在文件之间重复设置.
例如,Debian的默认~/.profile包含以下代码段:
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi因此,它显式地获取~/.bashrc,因此登录和非登录交互shell都将包括添加到该文件中的自定义。
https://unix.stackexchange.com/questions/439042
复制相似问题