Linux环境变量加载的研究

我们经常遇到在linux执行某条命令时出现xxx文件没找到的问题。很多情况都不是库没有安装,而是环境变量的错误。

但是,我明明是设置了环境变量啊。所以,我对此进行了试验。

我们登录linux有很多种,bash来交互式执行,或者直接非交互式执行命令。在我的试验后,发现,原来这几个的环境变量加载都是不同的。

相关文件:

从电脑上,我找到这么几个相关文件。

1/etc/profile
2/etc/environment
3/etc/bashrc
4~/.bash_profile
5~/.bashrc   
6~/.bash_logout

bash登录方式和环境变量的关系:

环境加载和4种bash模式相关。

什么是交互式shell(interactive shell)和非交互式shell(non-interactive shell):

交互式的shell会有一个输入提示符,并且它的标准输入、输出和错误输出都会显示在控制台上。所以一般来说只要是需要用户交互的。

非交互式shell是 bash script.sh 这类的shell,脚本或程序执行完就结束了,没有交互。

登录式shell(login shell)和非登陆式shell(no-login shell):

需要输入用户名和密码的shell就是登陆式shell。因此通常不管以何种方式登陆机器后用户获得的第一个shell就是login shell。不输入密码的ssh是公钥打通的,某种意义上说也是输入密码的。

非登陆式的就是在登陆后启动bash等,即不是远程登陆到主机这种。

加载情况表:

文件非交互+登陆式交互+登陆式交互+非登陆式非交互+非登陆式
/etc/profile加载加载  
/etc/environment   
/etc/bashrc加载加载  
~/.bash_profile加载加载  
~/.bashrc加载加载加载 
BASH_ENV   加载

相关场景:

  • 登陆机器后的第一个shell:login + interactive
  • 新启动一个shell进程,如运行bash:non-login + interactive
  • 执行脚本,如bash script.sh:non-login + non-interactive
  • 运行头部有如#!/usr/bin/env bash的可执行文件,如./executable:non-login + non-interactive
  • 通过ssh登陆到远程主机:login + interactive
  • 远程执行脚本,如ssh user@remote script.sh:non-login + non-interactive
  • 远程执行脚本,同时请求控制台,如ssh user@remote -t 'echo $PWD':non-login + interactive
  • 在图形化界面中打开terminal:
  • Linux上: non-login + interactive
  • Mac OS X上: login + interactive

准备:

我在每个文件的开头和结尾都加了一句脚本。

1echo 脚本名 begin
2xxxx脚本内容xxxx
3echo 脚本名 end

实验:

下面显示输出的情况,用表格来隔开,以显示递归的情况。

直接ssh连上,登录:

交互式登录式shell

/etc/profile begin
/etc/profile end
~/.bash_profile begin
~/.bashrc begin
/etc/bashrc begin
/etc/bashrc end
~/.bashrc end
~/.bash_profile end

 在已经登陆后的终端,执行bash命令:

交互式登录式shell

/root/bashrc begin
/etc/bashrc begin
/etc/bashrc end
/root/bashrc end

在已经登陆后的终端,执行bash -l命令:

交互式登陆式shell

/etc/profile begin
/etc/profile end
/root/bash_profile begin
/root/bashrc begin
/etc/bashrc begin
/etc/bashrc end
/root/bashrc end
/root/bash_profile end

su命令到另一个用户:

交互式登录式shell

~/.bashrc begin
/etc/bashrc begin
/etc/bashrc end
~/.bashrc end

普通用户下sudo ls:

非交互式非登陆shell

没有输出变量信息。

bash -l -c “ls”命令:

非交互式登录式shell

/etc/profile begin
/etc/profile end
~/.bash_profile begin
~/.bashrc begin
/etc/bashrc begin
/etc/bashrc end
~/.bashrc end
~/.bash_profile end

远程 ssh sean@test ls 命令:

非交互式登陆式shell

~/.bashrc begin
/etc/bashrc begin
/etc/bashrc end
~/.bashrc end

一些结论:

其实从上面的显示中,我们可以看出,有几个文件有调用关系。

调用关系:

~/.bash_profile -> ~/.bashrc -> /etc/bashrc

其实去查看它们的代码就能发现,里面有执行的语句。

注意 bash -l :

加了-l参数后,打开的是登陆式shell。这要注意。

BASH_ENV变量:

一个环境变量,用于指定非交互+非登陆式的环境变量文件。

转载请注明:旅途@KryptosX » Linux环境变量加载的研究

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值