Is it possible to get the real username inside of a script that was called using sudo?

Solution 1:

sudo creates an environment variable called SUDO_USER. You can use it in your script like so:

echo $SUDO_USER

Solution 2:

You can look at the SUDO_USER, SUDO_UID or SUDO_GID environmental variables to find this information.