Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I always been using && but just saw someone uses ;.
What is the difference? When should I use ; or &&?

share|improve this question
    
There seems to be a big confusion here. ; and && are not equivalent. Please add the example where you saw this, it must make sense in the context of the problem –  Suvarna Apr 7 at 7:12
    
duplicate of unix.stackexchange.com/q/100704/63453 –  Jasper Apr 7 at 7:52

1 Answer 1

up vote 4 down vote accepted

The basic difference is that all commands separated by ; would be executed whereas when commands are separated by && then the subsequent command would be executed only if the previous exited with a return code of 0.

As such, when you say:

command1; command2; command3

the shell executes command1, then command2 and when the latter is also complete command3.

On the other hand,

command1 && command2 && command3

causes command1 to be executed. If it is successful, i.e. it exits with a return code of 0, then command2 is executed. Similarly, command3 is executed if the previous command executed successfully.

The latter construct is useful when you want to execute commands conditionally.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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