609

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?
4
  • 13
    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, 2013 at 22:11
  • 5
    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, 2013 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. Jun 24, 2017 at 5:23

7 Answers 7

1110

Cheatsheet:

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.
10
  • 38
    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. Oct 14, 2015 at 8:58
  • 7
    is it possible to say: run a in background, followed by b in background only if a worked? ( I guess &&& ?)
    – user230910
    Oct 23, 2015 at 4:29
  • 25
    @user230910: that would be (A && B) &. Dec 23, 2015 at 23:09
  • 8
    Can you reference authoritative document for this?. Aug 14, 2016 at 20:27
  • 5
    @JaimeHablutzel See man bash under SHELL GRAMMAR -> Lists
    – wjandrea
    May 6, 2018 at 3:56
94

&& 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
fi
2
  • 1
    Thanks. I have updated the question to make sure the different subquestions are easily distinguishable.
    – don.joey
    Aug 20, 2013 at 20:51
  • 6
    @Private: You should use ; if the second command does not need the previous one to succeed.
    – choroba
    Aug 21, 2013 at 11:43
40

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

Using && will execute the second 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 &&.

0
18

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.

11

Jack's answer is very good rule of thumb.

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.

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.

Examples:

  • Concatenate the output of two commands:

    (ls foo; ls bar) > single-result.txt
    
  • Go into a directory and perform a command from there without changing the current directory of the shell:

    (cd target-directory && jar -xf ../my-jar)
    
8

Kudos to Jack's cheatsheet for saying how this works clearly and succinctly. But all of these things have a more general reason for being chosen. You don't just have to memorize these facts for when you're doing stuff in the shell. For more context you might also like to know that:

  • ; is commonly used as a statement separator in languages like C, C++, Perl, Java, C#, awk, and a slew of languages that have been derived from those. It performs the same function in all of those: do one thing, then the next. It is like a period at the end of a sentence.
  • & is used in C for bitwise AND so && was used for logical AND. Logically ANDing things in C includes a feature called shortcutting. Shortcutting means that for an AND that if the first thing is false there's no reason to check the second thing. A single false item in an AND means the whole thing will be false. In the shell that means it won't go onto the second command if the first one fails. Or if the first thing worked, it will go onto the second thing to see if it "might be false".
  • | is used in C for bitwise OR so || is used for logical OR. Logical OR's also feature shortcutting. In an OR this means that you look for the first true thing. So if the first thing fails it will try the second thing.
2
  • Why are most explanations for C (and stuff that has nothing to do) when the question is about bash?
    – CristiFati
    Jan 14, 2022 at 2:35
  • Because C was the origin of tons of stuff we still regularly use.
    – chicks
    Jan 14, 2022 at 5:08
5

Others have said it well. From a practical perspective, I often find that if you want to chain commands then you would like to know where something fails as well. For this you need to understand grouping in bash commands using the curly brackets.

Instead of a && b you will want to write:

a || { echo "a failed" && false; } && b || echo "b failed"
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .