Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

In tutorials and how-to's I often see commands combined. For instance,

sudo apt-get update && sudo apt-get install pyrenamer

There seem to be four possible connectors: &, &&, || and ;. Though the & connector is clear to me (it sends a process to the background and leaves the terminal available), it is not clear what the difference is between && and ;. And I did not know of || until Kaya's comment.

The following questions deal with the difference between the two connectors, but do so mostly in the comments:

So here are a number of related questions:

  1. What is the difference between ; and &&?
  2. When should you use them respectively? It would be nice to see some use cases: if I want to run a command and then after it shutdown my computer, which connector should I choose?
  3. What are their advantages and dangers? Robie Basak mentions in a comment to this answer that a command like cd /somewhere_else; rm -Rf * can have destructive consequences if the first element in the command chain fails, for instance.
  4. If relevant, where do they come from?
share|improve this question
3  
There is another connector that you may not have come across: || is the same as && except that it only executes the second command if the first one exited with a non-zero (unsuccessful) status. –  Kaya Aug 20 '13 at 22:11
1  
Also note that running your script with set -e will stop the script on failure as if all the commands were connected with &&. –  choroba Aug 21 '13 at 18:21
add comment

3 Answers

up vote 25 down vote accepted

&& only runs the second command if the first one exited with status 0 (was successful). ; run both the commands, even if the first one exits with a non zero status.

Your example with && can be equivalently paraphrased as

if sudo apt-get update ; then
    sudo apt-get install pyrenamer
fi
share|improve this answer
    
Thanks. I have updated the question to make sure the different subquestions are easily distinguishable. –  don.joey Aug 20 '13 at 20:51
3  
@Private: You should use ; if the second command does not need the previous one to succeed. –  choroba Aug 21 '13 at 11:43
add comment

Using ; will execute the commands irrespective whether first command is successful or not.

using && execute 2nd command only when first command executed successfully (status 0).

Both are used on different perspective. Like for a longer process, say for an installation you need to compile and install it. you should make && make install. So the install will run only if make successful.

So for dependent commands you should use &&

Wring bash, or commands with independent commands use ;

So if you want to shutdown computer even the first job failed use ; , but if want on complete success of first job initiate the shutdown use &&

share|improve this answer
    
Thanks. I like the last use case you present. I have updated the question. –  don.joey Aug 20 '13 at 20:52
add comment

a ; b will run b regardless of the exit status of a. a && b will run b only if a succeeded.

This is necessary and sufficient to answer to the first 3 questions. In particular, the 2 is too broad, and cannot be given "one" definitive answer - your best bet is to decide on a case by case basis.

As for the 4th question: They're Bash syntax.

There is no intrinsic danger in using either. Again, the definition above is sufficient. It implies that you will write && when b has unintended effects if a does not succeed. There is no need for further rules or explanation, IMHO.

share|improve this answer
add comment

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.