/etc/profile配置文件内容注释说明(shell脚本)

本文解析了Linux系统中/etc/profile文件的关键内容及其作用。介绍了如何通过修改此文件来定制用户的运行环境,包括环境变量的定义及路径设置等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

该配置文件是红帽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       #删除此脚本中用到的函数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值