If I run a command.
sudo some-command && some-other-command
Is the second command being run with sudo
previlege also?
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.
Sign up to join this communityTL;DR: NO
The first command is
sudo some-command
The second command is
some-other-command
The command sudo
takes the following command and executes it with elevated privileges. The &&
, however, doesn't belong to a command and is interpreted by the shell, before executing the first command. It tells bash to first execute the first command, and on success the second one.
So the sudo
doesn't know about the && some-other command
. When the elevated process terminates, bash takes its return value, and then executes the other command, which again doesn't know of the first one.
This can be demonstrated easily:
$ sudo whoami && whoami
root
username
To reach what you want, you can start an elevated bash, and let it execute both commands:
$ sudo bash -c 'whoami && whoami'
root
root
So now, both commands are executed as root, as the whole process described above is executed in an elevated process, i.e. the bash session started with this command, which immediately exits after finishing. Still, the both whoami
s don't know of each others existence.
This does not suit any purpose but for this demonstration, the easier way is to simply do
sudo some-command && sudo some-other-command
cmd1 && sudo cmd2
, except cmd1
locks the screen, meaning that when cmd2
tries to run with sudo
, and sudo
requests your password, there's no way to see the request nor type it in. How to solve this I wonder?
Jun 26 at 5:53
sudo true &&
is the secret.
Jun 26 at 6:21
No. Some examples that do this are:
sudo some-command && sudo some-other-command
sudo sh -c "some-command && some-other-command"
or if you want nest commands you can even do:
sudo bash <<"EOF"
some-command
some-other-command
sudo bash <<"EOF2"
some-command2
some-other-command2
EOF2
EOF
$ sudo -s -- whoami && whoami
gives root
username
Jun 10, 2015 at 15:26
sudo -s -- "whoami && whoami"
seems to give command not found, more or less the same as command "whoami && whoami"
. How to use this construction with &&? I see that sudo -s cd ..
gives no error as opposed to sudo cd ..
.
Jun 10, 2015 at 20:31
No, either you must write sudo twice, or do something like
sudo bash -c 'foo && bar'
&&
means that the right sided (second) command will only run only if the lest sided (first) command is successful i.e. exit code $?
is 0
.
The two commands are different from each other and will run in their own environments so the second command will not run having sudo
privilege.
This will make you clear:
$ sudo whoami && whoami
root
foobar
NO, and the following was once very commonly macro'd.
sudo reboot && exit
Just about everybody should have done this at least once
sudo apt-get update && sudo apt-get upgrade
Thus it is a great "mickey mouse" interview question.
This is so easily tested that the question may be suspected to a stat padding exercise.
On the command line, when you see
$ command one && command two
the typical intent is to execute the command that follows the && only if the first command is successful.
absolutely no, this is just as writing two consecutive commands, this && gives no added privilege to a command. It's just a seperator.
To prove that let us take an example.
touch fileA fileB
I created two files fileA and fileB. Now I'll run the command
sudo chown test:test fileA && chown test:test fileB
I'm changing user and group owner of these files to user and group name test. Now the first command should run while what about the other?
The output is:
chown: changing ownership of `fileB': Operation not permitted
So the command didn't run since it needs sudo
and though we can conclude that && is not a replacement of the second sudo