Skip to content

一、shell 配置文件的加载规则:

  • 登录shell :完整加载系统和用户的登录配置
  • 交互式shell :主要加载交互配置
  • 非交互式shell (脚本):默认仅加载 $BASH_ENV 指定的文件
  • 交互式 shell vs 非交互式 shell :
  • 交互式 shell(如终端中直接运行的 shell)会加载更多的配置文件
  • 非交互式 shell(如脚本执行)通常只加载基本配置

二、zsh 配置文件加载规则:

  1. 登录式加载顺序:
  • /etc/zshenv (系统级、总是加载)
  • ~/.zshenv (用户级、总是加载)
  • /etc/zprofile (系统级、登录 shell 加载)
  • ~/.zprofile (用户级、登录 shell 加载)
  • /etc/zlogin (系统级、登录 shell 加载)
  • ~/.zlogin (用户级、登录 shell 加载)
  • ~/.zlogout (用户级、退出登录 shell 时加载)
  • /etc/zlogout (系统级、退出登录 shell 时加载)
  1. 非登录交互式加载顺序:
  • /etc/zshenv (系统级、总是加载)
  • ~/.zshenv (用户级、总是加载)
  • /etc/zshrc (系统级、交互式 shell 加载)
  • ~/.zshrc (用户级、交互式 shell 加载)
  1. 非交互式:
  • 通常只加载 /etc/zshenv 和 ~/.zshenv
  • 不会加载 .zshrc、.zprofile 等

三、bash 配置文件的加载规则:

  1. 登录式:
  • /etc/profile # 系统级登录配置
  • ~/.bash_profile # 用户级登录配置(优先)

→ 若不存在,尝试加载 ~/.bash_login → 若仍不存在,尝试加载 ~/.profile

  • **~/.bashrc # 通常在 ~/.bash_profile 中显式 source(非必须) **
  1. 非登录交互式:
  • /etc/bash.bashrc # 系统级交互配置(部分系统)
  • ~/.bashrc # 用户级交互配置
  1. 非交互式:(脚本执行)
  • 默认不加载配置文件
  • 仅加载 $BASH_ENV 环境变量指定的文件(若已设置)
  • 脚本中可显式 source 其他配置文件

四、环境变量与别名的作用域** :**

  • 在登录配置中定义的 环境变量 (通过 export )会传递给所有子进程
  • 在交互配置中定义的 别名 ( alias )和 函数 仅在当前shell有效

Released under the MIT License.