Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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
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
up vote 182 down vote accepted


A; B    Run A and then B, regardless of success of A
A && B  Run B if A succeeded
A || B  Run B if A failed
A &     Run A in background.
share|improve this answer
this is the best answer. thanks! – Federico Jan 7 '15 at 18:04
And of course, A & B &: Run A in background, then run B in background (regardless of success) and return control to the shell. This often works about the same as running both processes at the same time. – Limited Atonement Oct 14 '15 at 8:58
is it possible to say: run a in background, followed by b in background only if a worked? ( I guess &&& ?) – user230910 Oct 23 '15 at 4:29
@user230910: that would be (A && B) &. – leftaroundabout Dec 23 '15 at 23:09

&& only runs the second command if the first one exited with status 0 (was successful). ; runs 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
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
@Private: You should use ; if the second command does not need the previous one to succeed. – choroba Aug 21 '13 at 11:43

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

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

This works very well for me:

echo "update-grub
add-apt-repository ppa:sandromani/gimagereader
apt-get -y update
apt-get -y upgrade
apt-get -y install -f
apt-get -y install gpm
apt-get -y install shotwell
apt-get -y install aisleriot
apt-get -y install gnome-cards-data
apt-get -y install kshisen
apt-get -y install kmahjongg
apt-get -y install pokerth
apt-get -y install xsane
apt-get -y install mplayer
apt-get -y install rhythmbox
apt-get -y install gimagereader tesseract-ocr tesseract-ocr-eng
sh Setup
share|improve this answer
Could you please elaorate your answer to show how this is an answer to the question and if so why it's the way to go.. – heemayl Apr 30 '15 at 21:32

Your Answer


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.