This question already has an answer here:

What is the difference between

echo "Hello " ; echo "world"


echo "Hello " && echo "world"

Both seems to run the two commands after each other.

marked as duplicate by Gilles bash Feb 28 '15 at 21:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    In the second case, (&&), echo "world" is only executed if and only if echo "Hello " returns an exit status of zero. – jasonwryan Feb 27 '15 at 8:00
  • 2
    To illustrate the difference, try test -f /etc/passwdx && echo hello versus test -f /etc/passwdx ; echo hello (you may substitute any file you want, existing or non-existing). – a CVn Feb 27 '15 at 10:46
  • 2
    While I agree that the other question cited is, perhaps, technically a duplicate of this one, I contend that the wording of this question is far more useful to new users of Linux, who would not have the experience or knowledge to search for "control and redirection operators". – Wilson F Jun 5 '18 at 23:55

echo "Hello " ; echo "world" means run echo "world" no matter what the exit status of the previous command echo "Hello" is i.e. echo "world" will run irrespective of success or failure of the command echo "Hello".

Whereas in case of echo "Hello " && echo "world", echo "world" will only run if the first command (echo "Hello") is a success (i.e. exit status 0).

The following commands give an example of how the shell handles commands chaining using the different operators:

$ false ; echo "OK"
$ true ; echo "OK"
$ false && echo "OK"
$ true && echo "OK"
$ false || echo "OK"
$ true || echo "OK"
  • 28
    Nobody seems to mention that "&&" like in most programming languages means logical "and" and "||" is logical "or". The second one doesn't have to be evaluated to know that "false and whatever = false", so it just isn't evaluated. That's standardized behaviour of logical operators in almost all languages - the second operand is only evaluated if its true/false value makes a difference. The side effect is, that you can write stuff like test && echo success || echo fail. – orion Feb 28 '15 at 14:20
  • 7
    And that common programming language behavior of halting evaluation of logical operators if the result is already determined is called "short circuit" evaluation. – Will Feb 10 '16 at 18:30
  • Your answer has a couple of grammar errors; grammar errors disrupt the flow for many people, including myself, and such prose would never pass proofreading for a professional publication. Strange that you'd want to reject the edit. – Don Hatch Nov 16 '16 at 4:02

Every command in Linux returns an exit code when it finishes running. The exit code is assigned to a special variable ?, so you can easily check the status of last command, e.g. by echo $?. This is often utilised in scripts. If the command finishes successfully, it returns an exit code 0, whereas if there are any errors during the execution, the exit status is non-zero. See example below:

$ echo "Hello"
$ echo $?

$ rm nonexistent-file
rm: cannot remove ‘nonexistent-file’: No such file or directory
$ echo $?

Now, this leads us to your question. The && is a special operator that says 'execute the next command only if the previous command was successful, i.e. returned with an exit code of zero'. There is also a reverse operator ||, which only executes the following command if the previous command failed, i.e. exited with non-zero. See example below:

$ echo "Hello" && echo "Previous command was successful"
Previous command was successful

$ rm nonexistent-file && echo "This will not be printed"
rm: cannot remove ‘nonexistent-file’: No such file or directory

$ rm nonexistent-file || echo "But this will be shown"
rm: cannot remove ‘nonexistent-file’: No such file or directory
But this will be shown

EDIT: The qustion was about the difference betweem && and ; so for this to be a full answer I need to add that a semicolon simply divides one command from another. No checking of the exit code takes place, so if you have several commands separated by semicolons, they are simply executed sequentially, i.e. one after another, completely independently from each other.


In addition to @heemayl's answer, && (and also ||) will result in the shell only handling a single exit code for all the chained commands. So if you have a trap [...] ERR or set -o errexit line it will process all the commands before doing the exit code handling.

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