What's the difference between semicolon and double ampersand &&

What is the difference between

echo "Hello " ; echo "world"


echo "Hello " && echo "world"

Both seems to run the two commands after each other.

Asked By: lindhe


echo "Hello" ; echo "world" means sequentially run echo "world" no matter the exit status of the previous command echo "Hello". In other words, echo "world" will run irrespective of success or failure of the prior command echo "Hello".

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). This is called short circuit evaluation.

The following examples show how the shell handles command chaining with different operators:

$ false ; echo "OK"
$ true ; echo "OK"
$ false && echo "OK"
$ true && echo "OK"
$ false || echo "OK"
$ true || echo "OK"

The final case doesn’t print OK because it was short circuited by true.

In short, ; is a separator, && is logical AND, || is logical OR (read more here).

Answered By: heemayl

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.

Answered By: l0b0

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.

Answered By: Erathiel
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.