su在 RHEL7 上,当我使用shell/bin/sh或作为 shell 的用户时,我看到启动脚本有不同的行为/bin/bash,尽管事实/bin/sh表明/bin/bash
我已经为用户设置了以下脚本:
/home/my_user/.profile
echo Hello from .profile
Run Code Online (Sandbox Code Playgroud)
/home/my_user/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH
echo Hello from .bash_profile
Run Code Online (Sandbox Code Playgroud)
/home/my_user/.bashrc
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=
# User specific aliases and functions
echo Hello from .bashrc
Run Code Online (Sandbox Code Playgroud)
现在,当我su指定时/bin/bash,我得到以下输出。
[batwad@hellodave ~]$ sudo su -s /bin/bash my_user
Hello from .bashrc
[my_user@hellodave batwad]$
Run Code Online (Sandbox Code Playgroud)
这是.bash_profile没有被处决的意思吗?毕竟,我没有看到“Hello from .bash_profile”
现在,当我su指定/bin/sh(无论如何链接到/bin/bash)时,我得到以下输出。
[batwad@hellodave ~]$ sudo su -s /bin/sh my_user
sh-4.2$
Run Code Online (Sandbox Code Playgroud)
没有回声和不同的 shell 提示符。是什么赋予了?
根据 redseven 的回答,我尝试添加-l并得到了另一种行为!
[bawtad@hellodave ~]$ sudo su -l -s /bin/sh my_user
Last login: Thu Aug 16 11:44:38 UTC 2018 on pts/0
Hello from .profile
-sh-4.2$
Run Code Online (Sandbox Code Playgroud)
.profile这次用的就是这个!请注意,第一次尝试时并未出现“上次登录”部分。
您看到的行为差异是一个标准功能,以bash确保与普通标准 Bourne shell 环境完全兼容。
bash来自标准手册页(类型)的口/INVOCATION:
如果使用 sh 名称调用 bash,它会尝试尽可能模仿 sh 历史版本的启动行为,同时也符合 POSIX 标准。