Unix & Linux: What's the difference between semicolon and double ampersand && (3 Solutions!!)
Channel: Roel Van de Paar
Date: 14/08/2020

Unix & Linux: What's the difference between semicolon and double ampersand &&


The Question: What is the difference between
echo "Hello " ; echo "world"
and
echo "Hello " && echo "world"
Both seems to run the two commands after each other.

Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful

== This solution helped 197 people ==
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"
OK
$ true ; echo "OK"
OK
$ false && echo "OK"
$ true && echo "OK"
OK
$ false || echo "OK"
OK
$ true || echo "OK"
$

== This solution helped 8 people ==
In addition to https://unix.stackexchange.com/a/187148/3645, && (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.

With thanks & praise to God, and with thanks to the many people who have made this project possible! | Content (except music & images) licensed under cc by-sa 3.0 | Music: https://www.bensound.com/royalty-free-music | Images: https://stocksnap.io/license & others | With thanks to user lindhe (https://unix.stackexchange.com/users/33928), user l0b0 (https://unix.stackexchange.com/users/3645), user heemayl (https://unix.stackexchange.com/users/68757), user Erathiel (https://unix.stackexchange.com/users/65554), user Braiam (https://unix.stackexchange.com/users/41104), and the Stack Exchange Network (http://unix.stackexchange.com/questions/187145). Trademarks are property of their respective owners. Disclaimer: All information is provided "AS IS" without warranty of any kind. You are responsible for your own actions. Please contact me if anything is amiss at Roel D.OT VandePaar A.T gmail.com.