You can use the '& &' operator to execute the cmd2 command if and only if cmd1 was executed without errors:
(cmd1 && cmd2)
But this only works in bash directly, without the "sudo" in front.
So, to work as expected, we can use the following command:
sudo /bin/sh -c "apt-get update && apt-get dist-upgrade && apt-get autoremove && apt-get autoclean"
Please note that the answer proposed by amra does not match the command above: Commands separated by the ";" are executed sequentially, not taking into account the exit code of the previous command. When using & & to separate commands, the exit code is taken into account. Thus, if we have "cmd1 & cmd2", cmd2 is executed only if the exit code of cmd1 is 0 (i.e. Cmd1 is not interrupted).
Javaguru
source share