I was always thinking that to execute commands with sudo
and to execute bash script with sudo
were the same. But today I find that they are different. Here is what I've tested:
me@my_machine:~$ sudo echo $(whoami)
I execute the command from terminal and get the output: me
. After reading this link: Why does “sudo -u root echo whoami
” not return root, I understand this.
Then I create a bash script named test.sh
:
#!/usr/bin/env bash echo $(whoami)
And I execute this script: sudo ./test.sh
, to my surprise, the output now becomes: root
.
So executing commands with sudo
and executing Bash scripts with sudo
are different?
If I have a Bash script, containing the command whoami
, have it be executed with sudo
, how could I get me
, instead of root
?