Home‎ > ‎Basics‎ > ‎

Running multiple commands at once - command line

To run multiple commands at once, enter them into the command line separated with semicolon, && or ||. 

Using semicolon

When using semicolon, commands are executed in left to right order one after another regardless of the success of the predecessor command. It means, that if one of the command fails, following command will be still executed. 

$ rsync -avP work:/home/user/backup/ /home/user/downloads/backup/; sudo shutdown -h now

Even if rsync fails, computer will shutdown, but BEWARE: if you cancel this line with Ctrl + C, computer will shutdown. 

NOTE: This is applicable in current bash version, installed by default on Ubuntu, but in some type of mostly older shells Ctrl + C will cancel both jobs.

Using && or ||

&& continues to execute command on right only if the one on left is success. || is equivalent of OR, but in this case it executes command on right if one on left fails.

$ rsync -avP work:/home/user/backup/ /home/user/downloads/backup/ && sudo shutdown -h now

When rsync fails, computer will not shutdown.

$ rsync -avP work:/home/user/backup/ /home/user/downloads/backup/ || sudo shutdown -h now

$ rm file || echo 'Could not remove file!' > /var/log/my-custom.log

Only when rsync fails, computer will shutdown. This is contingent on command on the left side failing. In the second example if removing file fails, write a error message into the log file.