Difference Between && and ; chaining operators in Linux

Logical AND operator(&&):

The second command will only execute if the first command has executed successfully i.e, its exit status is zero. This operator can be used to check if the first command has been successfully executed. This is one of the most used commands in the command line.

Syntax:

command1 && command2

 

command2 will execute if command1 has executed successfully. This operator allows us to check the exit status of command1.

Semi-Colon Operator(;):

It is used to execute multiple commands in one go. Several commands can be chained together using this operator. The execution of the command succeeding this operator will always execute after the command preceding it has executed irrespective of the exit status of the preceding command. The commands always execute sequentially. Commands separated by a semicolon operator are executed sequentially, the shell waits for each command to terminate in turn. The return status is the exit status of the last command executed.

Syntax:

command1 ; command2

 

The execution of the second command is independent of the exit status of the first command. If the first command does not get successfully executed, then also the second command will get executed.

 

 

 

 

 

Â