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.
whoami
. Note no spaces and no cap I (whoamI is NOT the same as whoami). The commandwho
is working as expected, what output were you expecting ? – Panther May 09 '18 at 06:22who am i
is synopsis ofwho
, please check the section OPERANDS inman who
. I knowwhoami
is different command. I'm expecting some any output ofwho -m
orwho am i
, but there is no output. I updated the question. – pa4080 May 09 '18 at 06:30man who
in my desktop 16.04 LTS :If ARG1 ARG2 given, -m presumed: 'am i' or 'mom likes' are usual
but obviously those arguments and-m
are not implemented there ;-) – sudodus May 09 '18 at 06:38which who
is this? It's working fine for me on Lubuntu 16.04, Server 16.04 and Kubuntu 18.04 – I meanwho -m
works, notsudo who -m
returning “root”. – dessert May 09 '18 at 07:21which who
returns/usr/bin/who
on each of these systems. But on both Desktop systems there is no output when I typewho -m
. – pa4080 May 09 '18 at 08:41tty
, on Ubuntu Desktop,who -m
(who am i
) works. Whatever, the accepted answer provides a solution of my actual problem. – pa4080 May 09 '18 at 16:58