63

If I run a command.

sudo some-command && some-other-command

Is the second command being run with sudo previlege also?

109

TL;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 whoamis 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
19

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
  • The 2nd uses another shell.
  • Mind that when you use ' or " in the 2nd version you need to escape those in the commands or parameters of that command (ie. \' or \") so that can get complex very quickly.
  • Third method does not work: $ sudo -s -- whoami && whoami gives root username – BartekChom Jun 10 '15 at 15:26
  • @BartekChom sorry I missed the quotes on that one – Rinzwind Jun 10 '15 at 17:22
  • 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 ... – BartekChom Jun 10 '15 at 20:31
  • I've learnt something new today: sudo bash <<"EOF"! Thanks! :-) – Fabby Jun 12 '15 at 9:50
4

No, either you must write sudo twice, or do something like

sudo bash -c 'foo && bar'
2

&& 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
1

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.

0

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.

Is the other command runs as sudo also?

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.