一个脚本测试系统环境变量配置文件的启动顺序

咱们都知道CentOS系统中有四个定义环境变量的配置文件,分别是/etc/profile和/etc/bashrc以及家目录下的~/.bashrc和~/.bash_profile,那么咱们系统启动时是以什么顺序来读取这些环境变量配置文件的呢?centos

对于这个问题咱们能够写一个脚原本测试:bash

#!/bin/bashide

##############################################################测试

# File Name: shift.shcentos7

# Version: V1.0server

# Author: li qianip

# Organization: it

# Created Time : 2017-10-28 15:21:08io

# Description:class

##############################################################

rm -f /tmp/shijian

until [ $# -eq 0 ]

do

sed -i '/^echo.*shijian/d' "$1"

sed -i "1i echo ${1}1 $\(date +%T%N\) >> /tmp/shijian" "$1"

sed -i "\$a echo ${1}2 $\(date +%T%N\) >> /tmp/shijian" "$1"

shift

done

reboot

[root@centos7_2 ~]# sh /server/scripts/shift.sh /etc/profile /etc/bashrc /root/.bashrc /root/.bash_profile 

执行完脚本,系统会自动重启,而后咱们会在/tmp目录下的shijian文件看到咱们要的结果

[root@centos7_2 ~]# cat /tmp/shijian

/etc/profile1 15:42:52818124419

/etc/profile2 15:42:53809866251

/root/.bash_profile1 15:42:53887264247

/root/.bashrc1 15:42:53971852375

/etc/bashrc1 15:42:54069565295

/etc/bashrc2 15:42:54118786824

/root/.bashrc2 15:42:54185749478

/root/.bash_profile2 15:42:54237113170

结论:系统在启动时加载环境变量配置文件的顺序是/etc/profile-->~/.bash_profile-->~/.bashrc-->/etc/bashrc,同时咱们也发现了在加载这些文件的同时还会调用其余文件,因此咱们在定义全局环境变量和局部环境变量时最好分别在统一在一个文件(例如全局统一在/etc/profile局部统一在~/.bash_profile或~/.bashrc)中定义,不然会有覆盖的风险!

相关文章
相关标签/搜索