shell配置文件生效顺序

本文介绍Linux操作系统shell的配置文件生效顺序。

1. 交互式与非交互式 Shell 介绍

类型含义典型场景
交互式 Shell用户可输入命令并立即看到输出终端登录、打开新终端、执行 bash
非交互式 Shell自动执行命令,无需用户输入运行脚本 bash script.sh、ssh host "cmd"

登录 Shell vs 非登录 Shell:

类型含义典型场景
登录 Shell需要认证登录的 ShellSSH 登录、终端登录、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. 关键点

  1. Bash 自动读取的入口

    • 登录 Shell:/etc/profile、~/.bash_profile(或备选)
    • 非登录交互 Shell:~/.bashrc
    • 非交互 Shell:$BASH_ENV
  2. /etc/bashrc 可能被加载两次

    • 登录 Shell 中:/etc/profile 调用一次,~/.bashrc 又调用一次。
    • 通常用 BASHRCSOURCED 变量防重复,但只防内部配置,块外代码仍会执行。
  3. ~/.bash_logout

    • 仅登录 Shell 退出时由 Bash 自动读取。
    • 常用于清屏、清理临时文件、记录退出时间等。
  4. ~/.bash_history

    • 不是启动脚本,是历史命令数据文件。
    • 交互式 Bash 启动时读入内存,退出时写入。
    • 默认路径由 HISTFILE 控制,通常为 ~/.bash_history。
  5. 配置文件之间的 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;
防重复靠变量,块外代码仍执行。