shell配置文件生效顺序
shell配置文件生效顺序
本文介绍Linux操作系统shell的配置文件生效顺序。
1. 交互式与非交互式 Shell 介绍
| 类型 | 含义 | 典型场景 |
|---|---|---|
| 交互式 Shell | 用户可输入命令并立即看到输出 | 终端登录、打开新终端、执行 bash |
| 非交互式 Shell | 自动执行命令,无需用户输入 | 运行脚本 bash script.sh、ssh host "cmd" |
登录 Shell vs 非登录 Shell:
| 类型 | 含义 | 典型场景 |
|---|---|---|
| 登录 Shell | 需要认证登录的 Shell | SSH 登录、终端登录、su - user |
| 非登录 Shell | 已登录后启动的子 Shell | 图形界面打开终端、执行 bash |
2. 涉及文件与作用
| 文件 | 作用 | 执行时机 |
|---|---|---|
/etc/profile | 系统级环境变量,遍历 /etc/profile.d/*.sh | 登录 Shell 启动 |
/etc/profile.d/*.sh | 模块化全局配置 | 由 /etc/profile 或 /etc/bashrc 调用 |
/etc/bashrc | 系统级别名、函数、Shell 选项 | 登录 Shell 可能经 /etc/profile 调用;非登录 Shell 经 ~/.bashrc 调用 |
~/.bash_profile | 用户级环境变量,通常调用 ~/.bashrc | 登录 Shell 启动 |
~/.bash_login | 同 ~/.bash_profile,备选 | 登录 Shell,仅当前面文件不存在时 |
~/.profile | 同 ~/.bash_profile,备选 | 登录 Shell,仅当前面文件不存在时 |
~/.bashrc | 用户级别名、函数、提示符 | 每次交互式 Bash 启动 |
~/.bash_logout | 用户级退出清理 | 登录 Shell 退出时 |
~/.bash_history | 历史命令数据文件 | 交互式 Bash 启动读入、退出写入 |
注意:Bash 只自动读取固定入口文件,文件内部再
source谁由脚本内容决定。并且如果环境变量写在文件开头或结尾,环境变量生效顺序也会有差别。
3. 执行顺序与调用关系
3.1 交互式登录 Shell
Bash 启动
├─ /etc/profile
│ ├─ /etc/profile.d/*.sh
│ └─ /etc/bashrc # 若 /etc/profile 中显式 source
├─ ~/.bash_profile # 或 ~/.bash_login,或 ~/.profile,只读第一个存在的
│ └─ ~/.bashrc # 通常由 ~/.bash_profile 显式 source
│ └─ /etc/bashrc # 可能二次加载,内部通常有防重复
│
└─ 退出登录
├─ ~/.bash_logout # 仅登录 Shell 退出时执行
└─ 写入 ~/.bash_history # 交互式 Shell 退出时保存历史
3.2 交互式非登录 Shell
Bash 启动
└─ ~/.bashrc
└─ /etc/bashrc
└─ /etc/profile.d/*.sh # 部分发行版会在 /etc/bashrc 中遍历
退出
└─ 写入 ~/.bash_history # 视配置,通常交互式 Shell 会保存
不读取:
/etc/profile~/.bash_profile~/.bash_login~/.profile~/.bash_logout(非登录 Shell 退出不执行)
3.3 非交互式 Shell
Bash 启动
└─ 仅检查 $BASH_ENV # 若设置则读取其指向文件
不读取上述任何配置文件,通常也不读写 ~/.bash_history。
4. 调用关系图(简化)
登录 Shell:
/etc/profile
├─ /etc/profile.d/*.sh
└─ /etc/bashrc
~/.bash_profile
└─ ~/.bashrc
└─ /etc/bashrc
退出:
~/.bash_logout
~/.bash_history
非登录交互 Shell:
~/.bashrc
└─ /etc/bashrc
└─ /etc/profile.d/*.sh(视发行版)
退出:
~/.bash_history
5. 关键点
-
Bash 自动读取的入口
- 登录 Shell:
/etc/profile、~/.bash_profile(或备选) - 非登录交互 Shell:
~/.bashrc - 非交互 Shell:
$BASH_ENV
- 登录 Shell:
-
/etc/bashrc可能被加载两次- 登录 Shell 中:
/etc/profile调用一次,~/.bashrc又调用一次。 - 通常用
BASHRCSOURCED变量防重复,但只防内部配置,块外代码仍会执行。
- 登录 Shell 中:
-
~/.bash_logout- 仅登录 Shell 退出时由 Bash 自动读取。
- 常用于清屏、清理临时文件、记录退出时间等。
-
~/.bash_history- 不是启动脚本,是历史命令数据文件。
- 交互式 Bash 启动时读入内存,退出时写入。
- 默认路径由
HISTFILE控制,通常为~/.bash_history。
-
配置文件之间的
source关系~/.bash_profile通常source ~/.bashrc~/.bashrc通常source /etc/bashrc/etc/profile可能source /etc/bashrc/etc/bashrc可能在非登录 Shell 中遍历/etc/profile.d/*.sh
6. 生效顺序速查表
| Shell 类型 | 启动执行顺序 | 退出执行 |
|---|---|---|
| 交互式登录 Shell | /etc/profile → /etc/profile.d/*.sh → /etc/bashrc → ~/.bash_profile → ~/.bashrc → /etc/bashrc(可能二次) | ~/.bash_logout → 写 ~/.bash_history |
| 交互式非登录 Shell | ~/.bashrc → /etc/bashrc → /etc/profile.d/*.sh(视发行版) | 写 ~/.bash_history |
| 非交互式 Shell | $BASH_ENV(若有) | 一般不写历史 |
7. 记忆口诀
登录 Shell 走 profile,非登录走 bashrc;
profile 常调 bashrc,bashrc 再调 profile.d;
退出登录读 bash_logout,历史命令存 bash_history;
防重复靠变量,块外代码仍执行。
评论