What I want is to find the name of the user that runs bash script, when this script is executed by sudo
. The only possible way, that I know, is to parse the output of who -m
(who am i
) in this way:
user@UbuntuServer:~$ cat who.sh
#!/bin/sh
whoami
echo $USER
who -m | awk '{print $1}'
user@UbuntuServer:~$ sudo ./who.sh
root
root
user
The problem is: In Ubuntu Desktop 16.04.4 who -m
(resp. who am i
) do nothing. Why?
Another question is why the Ubuntu's online manual page for who
is different from man who
executed either on Ubuntu Desktop or Server?
But these questions are not so important according to my goals. And as it is mentioned in the title, the main question is: How do I find which user executes the script when is used sudo
?
Practically, the original title - How do I find 'who am I' on Ubuntu Desktop? - is the same but wrongly asked question.