On RHEL7 I see a different behaviour with startup scripts when I su
to a user with /bin/sh
or /bin/bash
as the shell, despite the fact that /bin/sh
points to /bin/bash
I've got the following scripts set up for the user:
/home/my_user/.profile
echo Hello from .profile
/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
/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
Now when I su
and specify /bin/bash
I get the following output.
[batwad@hellodave ~]$ sudo su -s /bin/bash my_user
Hello from .bashrc
[my_user@hellodave batwad]$
Does this mean .bash_profile
wasn't executed? After all, I don't see "Hello from .bash_profile"
Now when I su
and specify /bin/sh
(which links to /bin/bash
anyway) I get the following output.
[batwad@hellodave ~]$ sudo su -s /bin/sh my_user
sh-4.2$
No echoes and a different shell prompt. What gives?
Update
Following redseven's answer I tried adding -l
and got yet another behaviour!
[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$
It's .profile
that is used this time! Note the "Last login" part didn't appear on the first try.