Inspired by the idea from @MarSoft but I changed the lines like the following:
USERNAME='desireduser'
COMMAND=$0
COMMANDARGS="$(printf " %q" "${@}")"
if [ $(whoami) != "$USERNAME" ]; then
exec sudo -E su $USERNAME -c "/usr/bin/bash -l $COMMAND $COMMANDARGS"
exit
fi
I have used sudo
to allow a password less execution of the script. If you want to enter a password for the user, remove the sudo
. If you do not need the environment variables, remove -E
from sudo.
The /usr/bin/bash -l
ensures, that the profile.d
scripts are executed for an initialized environment.