execute command with sudo and execute Bash script with sudo
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
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: https://superuser.com/questions/384544/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 s
Bash script, containing the command whoami
, have it be executed with sudo
, how could I get me
, instead of root
?
bash shell-script sudo
add a comment |Â
up vote
1
down vote
favorite
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: https://superuser.com/questions/384544/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 s
Bash script, containing the command whoami
, have it be executed with sudo
, how could I get me
, instead of root
?
bash shell-script sudo
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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: https://superuser.com/questions/384544/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 s
Bash script, containing the command whoami
, have it be executed with sudo
, how could I get me
, instead of root
?
bash shell-script sudo
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: https://superuser.com/questions/384544/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 s
Bash script, containing the command whoami
, have it be executed with sudo
, how could I get me
, instead of root
?
bash shell-script sudo
edited Jul 28 at 21:18
slmâ¦
232k65479649
232k65479649
asked Jul 28 at 10:59
Yves
693414
693414
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
For the shell to run the command
sudo echo $(whoami)
it must first figure out what arguments to call sudo
with. echo
is just a simple string, so the shell does nothing to it, but $(whoami)
is a command substitution that needs to be expanded. It does this by executing whoami
and replacing the command substitution with its output. It is then ready to call sudo
.
This means that sudo
will be executed with echo
and the output of whoami
(which was executed as your unprivileged user) as arguments.
If you want to run whoami
with sudo
, you can put it in a script and run the script with sudo
, as you've done, or you may use
sudo sh -c 'echo "$(whoami)"'
Here, the sh -c
script is run as root, so whoami
will also run as root.
The point is that the shell that runs whoami
is executing as the root user.
The shell that executes whoami
in the first command, sudo echo $(whoami)
, runs as your ordinary user, not root.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
For the shell to run the command
sudo echo $(whoami)
it must first figure out what arguments to call sudo
with. echo
is just a simple string, so the shell does nothing to it, but $(whoami)
is a command substitution that needs to be expanded. It does this by executing whoami
and replacing the command substitution with its output. It is then ready to call sudo
.
This means that sudo
will be executed with echo
and the output of whoami
(which was executed as your unprivileged user) as arguments.
If you want to run whoami
with sudo
, you can put it in a script and run the script with sudo
, as you've done, or you may use
sudo sh -c 'echo "$(whoami)"'
Here, the sh -c
script is run as root, so whoami
will also run as root.
The point is that the shell that runs whoami
is executing as the root user.
The shell that executes whoami
in the first command, sudo echo $(whoami)
, runs as your ordinary user, not root.
add a comment |Â
up vote
3
down vote
accepted
For the shell to run the command
sudo echo $(whoami)
it must first figure out what arguments to call sudo
with. echo
is just a simple string, so the shell does nothing to it, but $(whoami)
is a command substitution that needs to be expanded. It does this by executing whoami
and replacing the command substitution with its output. It is then ready to call sudo
.
This means that sudo
will be executed with echo
and the output of whoami
(which was executed as your unprivileged user) as arguments.
If you want to run whoami
with sudo
, you can put it in a script and run the script with sudo
, as you've done, or you may use
sudo sh -c 'echo "$(whoami)"'
Here, the sh -c
script is run as root, so whoami
will also run as root.
The point is that the shell that runs whoami
is executing as the root user.
The shell that executes whoami
in the first command, sudo echo $(whoami)
, runs as your ordinary user, not root.
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
For the shell to run the command
sudo echo $(whoami)
it must first figure out what arguments to call sudo
with. echo
is just a simple string, so the shell does nothing to it, but $(whoami)
is a command substitution that needs to be expanded. It does this by executing whoami
and replacing the command substitution with its output. It is then ready to call sudo
.
This means that sudo
will be executed with echo
and the output of whoami
(which was executed as your unprivileged user) as arguments.
If you want to run whoami
with sudo
, you can put it in a script and run the script with sudo
, as you've done, or you may use
sudo sh -c 'echo "$(whoami)"'
Here, the sh -c
script is run as root, so whoami
will also run as root.
The point is that the shell that runs whoami
is executing as the root user.
The shell that executes whoami
in the first command, sudo echo $(whoami)
, runs as your ordinary user, not root.
For the shell to run the command
sudo echo $(whoami)
it must first figure out what arguments to call sudo
with. echo
is just a simple string, so the shell does nothing to it, but $(whoami)
is a command substitution that needs to be expanded. It does this by executing whoami
and replacing the command substitution with its output. It is then ready to call sudo
.
This means that sudo
will be executed with echo
and the output of whoami
(which was executed as your unprivileged user) as arguments.
If you want to run whoami
with sudo
, you can put it in a script and run the script with sudo
, as you've done, or you may use
sudo sh -c 'echo "$(whoami)"'
Here, the sh -c
script is run as root, so whoami
will also run as root.
The point is that the shell that runs whoami
is executing as the root user.
The shell that executes whoami
in the first command, sudo echo $(whoami)
, runs as your ordinary user, not root.
edited Jul 28 at 11:39
answered Jul 28 at 11:17
Kusalananda
101k13199311
101k13199311
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f459029%2fexecute-command-with-sudo-and-execute-bash-script-with-sudo%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password