what is the difference between "command && command" and "command ; command"

18,323

Solution 1

&& is a logical operator. ; is simple sequencing.

In cmd1 && cmd2, cmd2 will only be run if cmd1 exits with a successful return code.

Whereas in cmd1; cmd2, cmd2 will run regardless of the exit status of cmd1 (assuming you haven't set your shell to exit on all failure in your script or something).

On a related note, with cmd1 || cmd2, using the || 'OR' logical operator, cmd2 will only be run if cmd1 fails (returns a non-zero exit code).

These logical operators are sometimes used in scripts in place of a basic if statement. For example,

if [[ -f "$foo" ]]; then mv "$foo" "${foo%.txt}.mkd"; fi

...can be more concisely achieved with:

[[ -f "$foo" ]] && mv "$foo" "${foo%.txt}.mkd"

Solution 2

Syntax

command1 && command2

command2 is executed if, and only if, command1 returns an exit status of zero (true). In other words, run command1 and if it is successfull, then run command2.

command1 ; command2

Both command1 and command2 will be executed regardless. The semicolon allows you to type many commands on one line.

Related:

command1 || command2

command2 is executed if, and only if, command1 returns a non-zero exit status. In other words, run command1 successfully or run command2.


Example

&& operator:

$ rm /tmp/filename && echo "File deleted"

; operator:

$ echo "foo" ; echo "bar"

|| operator:

$ cat /tmp/filename 2>/dev/null || echo "Failed to open file"

External Links

  1. Linuxtopia.org
  2. Tldp.org
Share:
18,323

Related videos on Youtube

evilsoup
Author by

evilsoup

Updated on September 18, 2022

Comments

  • evilsoup
    evilsoup over 1 year

    I see these two usage on Ubuntu "command && command" and "command ; command",
    e.g. apt-get update && apt-get upgrade

    What would differ if I use apt-get update; apt-get upgrade?
    I am not asking for this specific usage but in general what is the difference between these two usage?

    • Admin
      Admin almost 11 years
      Win command-line and batch have the same feature: & (simple sequencing), && (conditional AND) and || (conditional OR).
    • Admin
      Admin almost 11 years
      See also: Bash Reference Manual – List of commands. In general, there's nothing you can't find in the documentation; it's really worth looking at if you have a question about specific syntax elements.
    • Admin
      Admin almost 11 years
      @Karan And for completeness, bash (linux/Ubuntu) has || as well.
  • Della
    Della over 5 years
    I find it a little bit fallacious because from my understanding, successful return code means 0, which, when cast into bool, gives a logical false. So going by the philosophy of Mccarthy evaluation used in most languages, it should immediately return false rather than evaluating (running) the next statement.
  • Gordon Davisson
    Gordon Davisson almost 4 years
    @Della 0 corresponding to false and non-0 to true is just a convention; for command exit statuses, it makes more sense to use the opposite convention, so that's what the shell does.