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?
  • 7
    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
  • 4
    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
  • 3
    Nobody answered Qn 4... I suspect the behaviour of && and || was inspired by the C programming language. In the case of (x && y), if x evaluates to false, the whole expression must be false so a compiler could optimize out the evaluation of y, in case it was expensive. The modern C and C++ standards actually require this optimization, so programs can safely assume that y will not be evaluated if x is false. For instance, (ptr && ptr->days > 31) will not crash even if ptr is null. Also in C, statements end with ; regardless of whether there is another statement on the same line or not. – Kevin Jun 24 '17 at 5:23


A; B    # Run A and then B, regardless of success of A
A && B  # Run B if and only if A succeeded
A || B  # Run B if and only if A failed
A &     # Run A in background.
|improve this answer|||||
  • 22
    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
  • 5
    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
  • 18
    @user230910: that would be (A && B) &. – leftaroundabout Dec 23 '15 at 23:09
  • 5
    Can you reference authoritative document for this?. – Jaime Hablutzel Aug 14 '16 at 20:27
  • 2
    @JaimeHablutzel See man bash under SHELL GRAMMAR -> Lists – wjandrea May 6 '18 at 3:56

&& 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
|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
  • 5
    @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 &&

|improve this answer|||||

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.

|improve this answer|||||

A; B # Run A and then B, regardless of success of A

A && B # Run B if and only if A succeeded

A || B # Run B if and only if A failed

A & # Run A in background.

A very good rule of thumb. I would add that in some cases, using these commands in a subshell make sense when we want to consider them as a single unit or we don't want to couple some operations outcomes with the current shell.


-concatenate the output of two commands :

(ls foo; ls bar) > single-result.txt

-going into a directory and perform a command from there while not changing the current directory of the shell :

(cd target-directory && jar -xf ../my-jar)
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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