加入收藏 | 设为首页 | 会员中心 | 我要投稿 衡阳站长网 (https://www.0734zz.cn/)- 数据集成、设备管理、备份、数据加密、智能搜索!
当前位置: 首页 > 教程 > 正文

Linux环境变量加载原理解析

发布时间:2021-01-25 14:20:56 所属栏目:教程 来源:互联网
导读:环境变量的分类 环境变量可以简单的分成用户自定义的环境变量以及系统级别的环境变量。 用户级别环境变量定义文件:~/.bashrc、~/.profile(部分系统为:~/.bash_profile) 系统级别环境变量定义文件:/etc/bashrc、/etc/profile(部分系统为:/etc/bash_profil

 

环境变量的分类

环境变量可以简单的分成用户自定义的环境变量以及系统级别的环境变量。

  • 用户级别环境变量定义文件:~/.bashrc、~/.profile(部分系统为:~/.bash_profile)
  • 系统级别环境变量定义文件:/etc/bashrc、/etc/profile(部分系统为:/etc/bash_profile)、/etc/environment

另外在用户环境变量中,系统会首先读取~/.bash_profile(或者~/.profile)文件,如果没有该文件则读取~/.bash_login,根据这些文件中内容再去读取~/.bashrc。

测试Linux环境变量加载顺序的方法

为了测试各个不同文件的环境变量加载顺序,我们在每个环境变量定义文件中的第一行都定义相同的环境变量UU_ORDER,该变量的值为本身的值连接上当前文件名称。

需要修改的文件如下:

  • /etc/environment
  • /etc/profile
  • /etc/profile.d/test.sh,新建文件,没有文件夹可略过
  • /etc/bashrc,或者/etc/bash.bashrc
  • ~/.bash_profile,或者~/.profile
  • ~/.bashrc

在每个文件中的第一行都加上下面这句代码,并相应的把冒号后的内容修改为当前文件的绝对文件名。

export UU_ORDER="$UU_ORDER:~/.bash_profile"

修改完之后保存,新开一个窗口,然后echo $UU_ORDER观察变量的值:


  1. uusama@ubuntu:~$ echo $UU_ORDER 
  2.  
  3. $UU_ORDER:/etc/environment:/etc/profile:/etc/bash.bashrc:/etc/profile.d/test.sh:~/.profile:~/.bashrc 

可以推测出Linux加载环境变量的顺序如下:

  1. /etc/environment
  2. /etc/profile
  3. /etc/bash.bashrc
  4. /etc/profile.d/test.sh
  5. ~/.profile
  6. ~/.bashrc

Linux环境变量文件加载详解

由上面的测试可容易得出Linux加载环境变量的顺序如下,:

系统环境变量 -> 用户自定义环境变量

/etc/environment -> /etc/profile -> ~/.profile

打开/etc/profile文件你会发现,该文件的代码中会加载/etc/bash.bashrc文件,然后检查/etc/profile.d/目录下的.sh文件并加载。


  1. # /etc/profile: system-wide .profile file for the Bourne shell (sh(1)) 
  2. and Bourne compatible shells (bash(1), ksh(1), ash(1), ...). 
  3.  
  4. if [ "$PS1" ]; then
  5.   if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then
  6.     # The file bash.bashrc already sets the default PS1. 
  7.     # PS1='h:w$ '
  8.     if [ -f /etc/bash.bashrc ]; then
  9.       . /etc/bash.bashrc 
  10.     fi 
  11.   else
  12.     if [ "`id -u`" -eq 0 ]; then
  13.       PS1='# '
  14.     else
  15.       PS1='$ '
  16.     fi 
  17.   fi 
  18. fi 
  19.  
  20. if [ -d /etc/profile.d ]; then
  21.   for i in /etc/profile.d/*.sh; do 
  22.     if [ -r $i ]; then
  23.       . $i 
  24.     fi 
  25.   done 
  26.   unset i 
  27. fi 

其次再打开~/.profile文件,会发现该文件中加载了~/.bashrc文件。


  1. # if running bash 
  2. if [ -n "$BASH_VERSION" ]; then
  3.     # include .bashrc if it exists 
  4.     if [ -f "$HOME/.bashrc" ]; then
  5.   . "$HOME/.bashrc"
  6.     fi 
  7. fi 
  8.  
  9. set PATH so it includes user's private bin directories 
  10. PATH="$HOME/bin:$HOME/.local/bin:$PATH"

从~/.profile文件中代码不难发现,/.profile文件只在用户登录的时候读取一次,而/.bashrc会在每次运行Shell脚本的时候读取一次。

一些小技巧

可以自定义一个环境变量文件,比如在某个项目下定义uusama.profile,在这个文件中使用export定义一系列变量,然后在~/.profile文件后面加上:sourc uusama.profile,这样你每次登陆都可以在Shell脚本中使用自己定义的一系列变量。

也可以使用alias命令定义一些命令的别名,比如alias rm="rm -i"(双引号必须),并把这个代码加入到~/.profile中,这样你每次使用rm命令的时候,都相当于使用rm -i命令,非常方便。

 

(编辑:衡阳站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读