linux中profile的执行说明

本文详细解析Linux中各个Shell配置文件的作用、使用场景及读取顺序,包括/etc目录下的全局配置文件和用户目录下的个性化设置,以及如何在不同登录方式下影响Shell行为。
(1)/etc/profile
全局(公有)配置,不管是哪个用户,登录时都会读取该文件。
(2)/ect/bashrc
Ubuntu没有此文件,与之对应的是/ect/bash.bashrc它也是全局(公有)的,bash执行时,不管是何种方式,都会读取此文件。
(3)~/.profile
若bash是以login方式执行时,读取~/.bash_profile,若它不存在,则读取~/.bash_login,若前两者不存在,读取~/.profile。
另外,图形模式登录时,此文件将被读取,即使存在~/.bash_profile和~/.bash_login。
(4)~/.bash_login
若bash是以login方式执行时,读取~/.bash_profile,若它不存在,则读取~/.bash_login,若前两者不存在,读取~/.profile。
(5)~/.bash_profile
Unbutu默认没有此文件,可新建。
只有bash是以login形式执行时,才会读取此文件。通常该配置文件还会配置成去读取~/.bashrc。
(6)~/.bashrc
当bash是以non-login形式执行时,读取此文件。若是以login形式执行,则不会读取此文件。
(7)~/.bash_logout
注销时,且是longin形式,此文件才会读取。也就是说,在文本模式注销时,此文件会被读取,图形模式注销时,此文件不会被读取。
下面是在本机的几个例子:
1. 图形模式登录时,顺序读取:/etc/profile和~/.profile
2. 图形模式登录后,打开终端时,顺序读取:/etc/bash.bashrc和~/.bashrc
3. 文本模式登录时,顺序读取:/etc/bash.bashrc,/etc/profile和~/.bash_profile
4. 从其它用户su到该用户,则分两种情况:
(1)如果带-l参数(或-参数,--login参数),如:su -l username,则bash是lonin的,它将顺序读取以下配置文件:/etc/bash.bashrc,/etc/profile和~ /.bash_profile。
(2)如果没有带-l参数,则bash是non-login的,它将顺序读取:/etc/bash.bashrc和~/.bashrc
5. 注销时,或退出su登录的用户,如果是longin方式,那么bash会读取:~/.bash_logout
6. 执行自定义的shell文件时,若使用“bash -l a.sh”的方式,则bash会读取行:/etc/profile和~/.bash_profile,若使用其它方式,如:bash a.sh, ./a.sh,sh a.sh(这个不属于bash shell),则不会读取上面的任何文件。
7. 上面的例子凡是读取到~/.bash_profile的,若该文件不存在,则读取~/.bash_login,若前两者不存在,读取~/.profile。

Linux 系统中,`/etc/profile.d/` 目录下的脚本用于为所有用户设置环境变量或执行初始化命令(如添加 PATH、设置语言、启用工具等)。这些脚本由 shell 登录时自动加载,但它们的 **执行顺序** 是一个重要且常被忽视的问题。 --- ## ✅ `/etc/profile.d/` 脚本的执行顺序 ### 🔹 执行顺序:**按字母顺序(lexicographical order)** > `/etc/profile.d/*.sh` 脚本是 **按照文件名的字典序(ASCII 排序)升序执行** 的。 这意味着: ```bash /etc/profile.d/00-env.sh → 先执行 /etc/profile.d/a-path.sh → 其次 /etc/profile.d/z-alias.sh → 最后 ``` ### 📌 示例说明: 假设 `/etc/profile.d/` 下有如下脚本: ``` 10-java.sh 05-python.sh 99-local.sh 01-global.sh a-utils.sh ``` 实际执行顺序为: ```text 01-global.sh 05-python.sh 10-java.sh 99-local.sh a-utils.sh ``` 因为这是标准的字符串排序结果。 --- ## ✅ 这些脚本是如何被调用的? 通常在 `/etc/profile` 文件末尾会有类似以下代码: ```bash if [ -d /etc/profile.d ]; then for i in /etc/profile.d/*.sh; do if [ -r "$i" ]; then . "$i" fi done unset i fi ``` 这段代码会: 1. 遍历 `/etc/profile.d/*.sh` 所有 `.sh` 结尾的文件 2. 按通配符展开顺序读取 —— 即 **shell 的 glob 扩展顺序** 3. 使用 `.`(source)执行每个可读脚本 4. 展开顺序就是文件名的字母顺序 📌 因此:**控制文件名 = 控制执行顺序** --- ## ✅ 实际影响:为什么顺序很重要? ### ⚠️ 场景 1:PATH 依赖问题 比如你有两个脚本: - `python.sh` 添加了 `~/miniconda3/bin` 到 `PATH` - `global.sh` 设置默认 `PATH`,但不包含 conda 如果 `global.sh` 在 `python.sh` 之后运行,则你的 Conda 路径会被覆盖! ```bash # /etc/profile.d/global.sh export PATH="/usr/local/bin:/usr/bin:/bin" ``` ```bash # /etc/profile.d/python.sh export PATH="$HOME/miniconda3/bin:$PATH" ``` ❌ 如果 `global.sh` 后执行 → `conda` 命令不可用! ✅ 解决方案:命名成 `00-python.sh` 和 `99-global.sh` 来确保顺序。 --- ### ⚠️ 场景 2:变量覆盖 ```bash # lang.sh export LANG=en_US.UTF-8 ``` ```bash # custom-lang.sh export LANG=zh_CN.UTF-8 ``` 最终生效的是 **字母顺序靠后的脚本** 中定义的值。 --- ## ✅ 如何查看当前系统的执行顺序? 你可以使用下面这个命令模拟 shell 加载时的实际顺序: ```bash printf '%s\n' /etc/profile.d/*.sh | sort ``` 输出示例: ```text /etc/profile.d/00-coreutils.sh /etc/profile.d/01-prompt.sh /etc/profile.d/java.sh /etc/profile.d/node.sh /etc/profile.d/rvm.sh ``` 这正是系统加载的顺序。 > 注意:有些发行版只加载 `.sh` 文件,有些也支持 `.csh`(用于 csh/tcsh) --- ## ✅ 最佳实践建议 | 建议 | 说明 | |------|------| | ✅ 使用数字前缀控制顺序 | 如 `00-init.sh`, `10-path.sh`, `99-finish.sh` | | ✅ 避免冲突覆盖 | 不要随意重置 `PATH`、`LANG` 等全局变量 | | ✅ 只添加必要的环境 | 尽量使用追加方式修改变量:<br>`export PATH="/new/path:$PATH"` | | ✅ 确保脚本可读且以 `.sh` 结尾 | 否则不会被 sourced | | ✅ 测试登录行为 | 使用 `su - username` 或重新登录验证 | --- ## ✅ 补充说明:哪些 shell 会加载这些脚本? | Shell 类型 | 是否加载 `/etc/profile.d/*.sh` | 触发条件 | |-----------|-------------------------------|----------| | bash | ✅ 是 | 登录 shell(如 `bash -l` 或 `su -`) | | dash | ✅ 是(如果调用 `/etc/profile`) | 登录时 | | zsh | ✅ 是 | 默认读取 `/etc/profile` | | fish | ❌ 否 | 使用自己的配置体系(`config.fish`) | 📌 所以:**非登录 shell**(如打开终端模拟器 GNOME Terminal)可能不会触发 `/etc/profile`,而是直接读取 `~/.bashrc`。需要特别注意! --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值