How do I get the username from within a script that was called with sudo? In other words, I don't want root
but the actual username.
echo_user.sh
#!/usr/bin/env bash
echo whoami
Calling the script from user pikachu, I would expect it to print pikiachu and not root.
sudo ./echo_user.sh
There are lots of post from around 7 to 12 years ago with this exact question. None of them seem to work on Ubuntu 22.x. I'm simply trying to get the name of the user that called a script with sudo. Most of the old examples reference SUDO_USER
but the only way I've been able to get it to work is by running sh -c 'echo $SUDO_USER'
. echoing SUDO_USER from a script shows it's undefined.
Try this: