Howto switch / chage user id witin a bash script to execute commands in the same script?

Solution 1:

No. But you can use sudo to run a shell and use a heredoc to feed it commands.

#!/bin/bash
whoami
sudo -u someuser bash << EOF
echo "In"
whoami
EOF
echo "Out"
whoami