224

What is the difference between ampersand and semicolon in Linux Bash?

For example,

$ command1 && command2

vs

$ command1; command2
4
  • 6
    I answered this here: superuser.com/a/619019/107862 Sep 4, 2014 at 15:40
  • 19
    Regarding the off-topicness, last time I checked, bash was still a programming language, but I was wrong before and it could be some dried fruit candy. Apr 17, 2015 at 9:27
  • It is, and were you asking a question about a specific behavior or the right way to do a certain thing it would certainly be on topic. This falls into a greyer category of "question about the syntax of a language" which isn't as clearly a "programming" question as far as I'm concerned. Apr 17, 2015 at 11:41
  • 4
    bash is in some ways dry, and in some ways sweet. At the end of the day it is quite pleasant and digestible. It cannot, however, be easily stolen from babies.
    – code_monk
    Aug 16, 2015 at 14:21

4 Answers 4

263

The && operator is a boolean AND operator: if the left side returns a non-zero exit status, the operator returns that status and does not evaluate the right side (it short-circuits), otherwise it evaluates the right side and returns its exit status. This is commonly used to make sure that command2 is only run if command1 ran successfully.

The ; token just separates commands, so it will run the second command regardless of whether or not the first one succeeds.

5
  • 23
    is the double ampersand && different from a single ampersand & in bash? Jan 16, 2017 at 21:40
  • 38
    @CharlieParker & causes the command to be run in the background, so yes. "Run this in the background" is very different from "run this next command only if this other one succeeds."
    – cdhowie
    Jan 18, 2017 at 15:39
  • The command2 will only run command1 returned zero exit status, meaning it finished successfully..
    – Nik
    Jan 23, 2019 at 7:01
  • 6
    Does ; still wait for the first command to finish?
    – rfii
    Aug 13, 2020 at 23:55
  • 1
    @rfii Yes, but it executes the second command without regard for whether the first command succeeded. a;b means "run a and then run b." a&&b means "run a, and then run b only if a was successful."
    – cdhowie
    Aug 14, 2020 at 4:49
55

command1 && command2

command1 && command2 executes command2 if (and only if) command1 execution ends up successfully. In Unix jargon, that means exit code / return code equal to zero.

command1; command2

command1; command2 executes command2 after executing command1, sequentially. It does not matter whether the commands were successful or not.

11

The former is a simple logic AND using short circuit evaluation, the latter simply delimits two commands.

What happens in real is that when the first program returns a nonzero exit code, the whole AND is evaluated to FALSE and the second command won't be executed. The later simply executes them both in order.

1

cmd1 ; cmd2

... runs cmd1 then cmd2.

cmd1 && cmd2

... runs cmd2 if cmd1 exited with a code of zero.

Both yield a status code of the last executed command.

However, what other answers miss...

You might think that cmd1 && cmd2 and (cmd1 && cmd2) are identical. In fact, they're very different. As is which command fails without the parens.

Consider the following. The status codes are determined by uncommenting set -e

#!/bin/bash
set -e # exit on error
set -x

# false    # Yields status 1 and would cause the script to exit if -x is left uncommented

true && true     # Does not exit script! Good
echo A $?        # 0

false && true    # Does not exit script! Wut?
echo B $?        # 1

true && false    # Exits the script.  Wut? Wut?
echo C $?        # 1, if not -e

(false && true)  # Exits the script
echo D $?        # 1, if not -e

(true && false)  # Exits the script
echo E $?        # 1, if not -e

D and E make sense, and the difference with the others makes sense - parens creates a subshell. However that both of the following yield a status of 1, but that only the seconds exits the script is truly bizarre:

false && true  
true && false 

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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