← 计算机

shell的运行方式与环境变量加载

shell的运行方式与环境变量加载

文章封面

shell运行模式

login shell

系统加载(主动)

  1. /etc/profile
  2. ~/.bash_profile 或 ~/.bash_login 或 ~/.profile

脚本加载(被动)

一般加载步骤

  1. /etc/profile
  2. /etc/profile.d/*
  3. ~/.bash_profile 或 ~/.bash_login 或 ~/.profile
  4. ~/.bashrc
  5. /etc/bashrc

登录方式

non-login shell

系统加载(主动)

脚本加载(被动)

一般加载步骤

  1. ~/.bashrc
  2. /etc/bashrc
  3. /etc/profile.d/*

非登录方式

系统shell切换

判断当前shell状态

shopt |grep login_shell

# himBH交互式 hb非交互
echo $-

脚本加载相关代码

# 位于/etc/profile文件
for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then 
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

# 位于/etc/bashrc文件
for i in /etc/profile.d/*.sh; do
    if [ -r "$i" ]; then
        if [ "$PS1" ]; then
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done
#位于~/.bash_profile文件
if [ -f ~/.bashrc ]; then
	. ~/.bashrc
fi
# 位于~/.bashrc文件
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi
读到这里,感谢你的时间。继续阅读 →