为什么我使用交互式非登录 shell,但仍然可以访问 /etc/profile 中定义的环境变量?

为什么我使用交互式非登录 shell,但仍然可以访问 /etc/profile 中定义的环境变量?

根据bash手册,只有登录shell才会加载/etc/配置文件文件。

当 Bash 作为交互式登录 shell 或带有 --login 选项的非交互式 shell 被调用时,它首先从文件 /etc/profile 中读取并执行命令(如果该文件存在)。

我没有明确加载/etc/配置文件文件。

我想知道为什么我使用交互式非登录 shell,但仍然可以访问中定义的环境变量/etc/配置文件文件?

# current shell PID
[root@kafka100 cpp]# echo $$
1829

# indicates non-login shell
[root@kafka100 cpp]# shopt | grep login
login_shell     off

# JAVA_HOME was defined in /etc/profile
[root@kafka100 cpp]# echo $JAVA_HOME
/usr/local/src/jdk1.8.0_333

进程树如下:

[root@kafka100 cpp]# ps xjf
  PPID    PID   PGID    SID TTY       TPGID STAT   UID   TIME COMMAND
     1   1017   1017   1017 ?            -1 Ss       0   0:00 /usr/sbin/sshd -D
  1017   1109   1109   1109 ?            -1 Ss       0   0:00  \_ sshd: root@notty
  1109   1535   1535   1535 ?            -1 Ss       0   0:00      \_ bash
  1535   1582   1535   1535 ?            -1 S        0   0:00          \_ sh /root/.vscode-server/bin/30d9c6cd9483b2cc586687151bcbcd635f373630/bin/code-server --start-server --host=127.0.0.1 --accept-server-license-terms --enable-remote-auto-shutdown --port=0 --telemetry-level all --connection-token-file /root/.vscode-server/.30d9c6cd9483b2cc586687151bcbcd635f373630.token
  1582   1594   1535   1535 ?            -1 Rl       0   0:03          |   \_ /root/.vscode-server/bin/30d9c6cd9483b2cc586687151bcbcd635f373630/node /root/.vscode-server/bin/30d9c6cd9483b2cc586687151bcbcd635f373630/out/server-main.js --start-server --host=127.0.0.1 --accept-server-license-terms --enable-remote-auto-shutdown --port=0 --telemetry-level all --connection-token-file /root/.vscode-server/.30d9c6cd9483b2cc586687151bcbcd635f373630.token
  1594   1644   1535   1535 ?            -1 Sl       0   0:03          |       \_ /root/.vscode-server/bin/30d9c6cd9483b2cc586687151bcbcd635f373630/node /root/.vscode-server/bin/30d9c6cd9483b2cc586687151bcbcd635f373630/out/bootstrap-fork --type=ptyHost
  1644   1829   1829   1829 pts/1      2407 Ss       0   0:00          |       |   \_ /usr/bin/bash
[root@kafka100 cpp]# cat /proc/1644/environ | tr '\0' '\n'
XDG_SESSION_ID=1
SELINUX_ROLE_REQUESTED=
SHELL=/bin/bash
SSH_CLIENT=192.168.44.1 51182 22
SELINUX_USE_CURRENT_RANGE=
USER=root
VSCODE_AGENT_FOLDER=/root/.vscode-server
MAIL=/var/mail/root
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
PWD=/root
LANG=en_US.UTF-8
SELINUX_LEVEL_REQUESTED=
SHLVL=3
HOME=/root
LOGNAME=root
SSH_CONNECTION=192.168.44.1 51182 192.168.44.100 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
XDG_RUNTIME_DIR=/run/user/0
_=/root/.vscode-server/bin/30d9c6cd9483b2cc586687151bcbcd635f373630/node
VSCODE_HANDLES_SIGPIPE=true
VSCODE_PARENT_PID=1594
VSCODE_LAST_PTY_ID=0
VSCODE_AMD_ENTRYPOINT=vs/platform/terminal/node/ptyHostMain
VSCODE_PIPE_LOGGING=true
VSCODE_VERBOSE_LOGGING=true
VSCODE_RECONNECT_GRACE_TIME=10800000
VSCODE_RECONNECT_SHORT_GRACE_TIME=300000
VSCODE_RECONNECT_SCROLLBACK=100
NODE_CHANNEL_FD=3
NODE_CHANNEL_SERIALIZATION_MODE=json
[root@kafka100 cpp]# cat /proc/1829/environ | tr '\0' '\n'
XDG_SESSION_ID=1
SHELL=/bin/bash
SSH_CLIENT=192.168.44.1 51182 22
USER=root
MAIL=/var/spool/mail/root
PATH=/root/.vscode-server/bin/30d9c6cd9483b2cc586687151bcbcd635f373630/bin/remote-cli:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/src/jdk1.8.0_333/bin:/usr/local/src/kafka_2.12-3.3.1/bin:/usr/local/src/cmake-3.23.0-linux-x86_64/bin:/root/bin
PWD=/root/workspace/cpp
LANG=en_US.UTF-8
SHLVL=4
HOME=/root
LOGNAME=root
SSH_CONNECTION=192.168.44.1 51182 192.168.44.100 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
XDG_RUNTIME_DIR=/run/user/0
_=/root/.vscode-server/bin/30d9c6cd9483b2cc586687151bcbcd635f373630/node
KAFKA_HOME=/usr/local/src/kafka_2.12-3.3.1
HOSTNAME=kafka100
HISTSIZE=1000
CMAKE_HOME=/usr/local/src/cmake-3.23.0-linux-x86_64
JAVA_HOME=/usr/local/src/jdk1.8.0_333
HISTCONTROL=ignoredups
BROWSER=/root/.vscode-server/bin/30d9c6cd9483b2cc586687151bcbcd635f373630/bin/helpers/browser.sh
TERM_PROGRAM=vscode
TERM_PROGRAM_VERSION=1.68.1
COLORTERM=truecolor
VSCODE_IPC_HOOK_CLI=/run/user/0/vscode-ipc-5aa8f760-55f0-41a9-9077-f078504d0176.sock
TERM=xterm-256color

相关内容