SyntaxFix
Write A Post
Hire A Developer
Questions
Much simpler: use sudo to run a shell and use a heredoc to feed it commands.
sudo
#!/usr/bin/env bash whoami sudo -i -u someuser bash << EOF echo "In" whoami EOF echo "Out" whoami
(answer originally on SuperUser)