Difference Between &, &&, and ; in Bash

Difference Between &, &&, and ; in Bash

command1 & command2 will put command1 into the background and then run command2 Warning command2 will probably execute before command1 (just slightly).
command1 && command2 && is the Boolean AND operator so command2 will only run if command1 returns a zero exit code (ie. runs successfully)
command1; command2 ; is a command seperator, so it will run command1 then command2. So command2 will run even if command1 fails.

command1 & start command in background, using nohup command1 & to make sure it keeps running even if you close the terminal window. Apparently it’s called job control

eg. curl -O https://example.com/test.txt && rm test.txt
rm test.txt will only run if curl was successful

|| means OR
eg. curl -O https://example.com/test.txt || curl -O https://example.org/test.txt

Leave a Reply

Your email address will not be published. Required fields are marked *