文章目录
推荐:这个是我写的一些变量(局部、环境、系统、只读、位置变量) 和 环境变量的一些基础问题,点一下说不定会涨知识~~
环境变量文件的分类:
1、用户级别文件:
Centos 中: ~/.bashrc 、 ~/.bash_profile
Ubuntu 中: ~/.bashrc 、 ~/.profile
只对当前登录系统的用户有效!
2、系统级别文件:
Centos 中:/etc/bashrc 、/etc/profile 、/etc/environment、/etc/profile.d/*.sh
Ubuntu 中:/etc/bash.bashrc 、/etc/profile 、/etc/environment、/etc/profile.d/*.sh
对所有用户都生效!
这里具体的使用方法 和 他们的差别在上篇文章中有记录! 戳我去学习!不要嫌麻烦!
正常登录系统的时候,环境变量文件的加载过程:
特别注意: 什么是正常登录的时候,什么时候是不正常的呢?
正常 就是直接输入username+password ,login 的时候;
不正常 就是当我们 在root用户中 使用 su 命令 切换用户 的时候。
系统在加载了其他程序启动之后,这里就到了调用第一个环境变量文件的时候:
非正常登录系统的时候,环境变量文件的加载过程:
非正常登录的时候,是指在 root用户 中 使用 su 命令 切换其他用户,因为 系统在我们登录root用户 的时候,已经加载过一遍 环境变量的配置文件了,所以当你 切换用户登录系统的时候就不用重头重新加载环境变量文件了! 这个时候是这样加载的:
对 系统登录起作用 的环境变量配置文件详解:
注意:这讲的是跟 Linux登录系统起作用的 环境变量配置文件,在下一章节会讲 跟 Linux登录系统无关的 环境变量的配置文件!
戳我直达下一章节,先看下面的文章也行,文末也有去下一章的链接!
1、Centos:/etc/profile
①、 /etc/profile 文件脚本内容详解:
[root @ Centos ~]# vim /etc/profile
#/etc/profile
"# System wide environment and startup programs, for login setup"
#系统范围的环境和启动程序,用于登录设置
"# Functions and aliases go in /etc/bashrc"
#函数和别名进入/ etc / bashrc
"# It's NOT a good idea to change this file unless you know what you"
#除非知道自己的内容,否则更改此文件不是一个好主意
"# are doing. It's much better to create a custom.sh shell script in"
# 是这样。 最好在其中创建一个custom.sh shell脚本
"# /etc/profile.d/ to make custom changes to your environment, as this"
#/etc/profile.d/可以对您的环境进行自定义更改,如下所示
②、定义系统查找命令的路径:
[root @ Centos ~]# vim /etc/profile
pathmunge () {
case ":${PATH}:" in
*:"$1":*)
;;
*)
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
esac
}
.......
③、根据登录的用户的 UID 来判断是什么用户:
[root@Centos ~]# vim /etc/profile
if [ -x /usr/bin/id ]; then
if [ -z "$EUID" ]; then
# ksh workaround
EUID=`/usr/bin/id -u`
UID=`/usr/bin/id -ru`
fi
USER="`/usr/bin/id -un`"
LOGNAME