该配置文件是红帽7版本的,对于不同的版本主要内容都是相同的。Linux是一个多用户的操作系统。每个用户登录系统后,都会有一个专用的运行环境。通常每个用户默认的环境都是相同的,这个默认环境实际上就是一组环境变量的定义。用户可以对自己的运行环境进行定制,其方法就是修改相应的系统环境变量。常在/etc/profile文件中修改环境变量,在这里修改的内容是对所有用户起作用的。下面分析一下/etc/profile文件中各部分内容的含义和作用。
# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /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
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
pathmunge () {
case ":${PATH}:" in
*:"$1":*)
;;
*)
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
esac
}
#判断当前系统的PATH中是否有该命令的目录,如果没有,则判断是要将该目录放于PATH之前还是之后。
#"$1"是要添加的内容,"${PATH}"是环境变量路径,检查是否已存在,如果存在,则不操作;如果不存在,判断需要添加到前面还是后面。
if [ -x /usr/bin/id ]; then #判断/usr/bin/id是否存在且有可执行权限
if [ -z "$EUID" ]; then #判断变量$EUID是否为空字符串
# ksh workaround
EUID=`id -u`
UID=`id -ru`
fi
#检查变量EUID,如果为空且/usr/bin/id可执行,则初始化变量EUID和UID
USER="`id -un`"
LOGNAME=$USER
MAIL="/var/spool/mail/$USER"
fi
#通过/usr/bin/id命令,初始化变量USER LOGNAME MAIL
# Path manipulation
if [ "$EUID" = "0" ]; then #判断,如果是root用户,则将/sbin /usr/sbin /usr/local/sbin添加到PATH变量中
pathmunge /usr/sbin
pathmunge /usr/local/sbin
else
pathmunge /usr/local/sbin after
pathmunge /usr/sbin after
fi
HOSTNAME=`/usr/bin/hostname 2>/dev/null` #初始化HOSTNAME并设置系统默认情况下不产生core文件
HISTSIZE=1000 #表示当输入history命令(即上翻键找历史命令)时,最多显示多少命令
if [ "$HISTCONTROL" = "ignorespace" ] ; then
export HISTCONTROL=ignoreboth #在不想被记住的命令前面输入一个空格,该命令就不会出现在历史记录中
else
export HISTCONTROL=ignoredups #剔除历史记录的命令中连续的重复条目
fi
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
umask 002 #普通用户umask
else
umask 022 #root用户umask
fi
for i in /etc/profile.d/*.sh ; do #执行/etc/profile.d/目录下所有脚本文件
if [ -r "$i" ]; then
if [ "${-#*i}" != "$-" ]; then
. "$i"
else
. "$i" >/dev/null
fi
fi
done
unset i #删除此脚本中用到的变量
unset -f pathmunge #删除此脚本中用到的函数