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 community
The first command is
The second command is
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.
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
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
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
The two commands are different from each other and will run in their own environments so the second command will not run having
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