How do I get the correct $USER if I run a shell script with sudo ?
I run them as postinstall scripts in a Mac install package, where they are being sudo-ed automatically, but I need to do stuff with the username.
$HOME is correct, though. The inelegant method would be to extract the name from the home path, but I wonder if there is a natural way to do this.
I can't influence the way the scripts are being called, as it's an automatic call inside the installer.
On my system the variable $SUDO_USER
is set to the caller's user name.
You shouldn't extract the username from the ${HOME}
variable directly. It's being configured and not calculated. To Extract the username you could take a look into /etc/passwd
file, but this is very system dependent, e.g. sometimes you have to look into a LDAP directory or the entries are propagated through NIS ...