In tutorials and how-to's I often see commands combined. For instance,
sudo apt-get update && sudo apt-get install pyrenamer
There seem to be four possible connectors: &
, &&
, ||
and ;
. Though the &
connector is clear to me (it sends a process to the background and leaves the terminal available), it is not clear what the difference is between &&
and ;
. And I did not know of ||
until Kaya's comment.
The following questions deal with the difference between the two connectors, but do so mostly in the comments:
So here are a number of related questions:
- What is the difference between
;
and&&
? - When should you use them respectively? It would be nice to see some use cases: if I want to run a command and then after it shutdown my computer, which connector should I choose?
- What are their advantages and dangers? Robie Basak mentions in a comment to this answer that a command like
cd /somewhere_else; rm -Rf *
can have destructive consequences if the first element in the command chain fails, for instance. - If relevant, where do they come from?
Cheatsheet:
&&
only runs the second command if the first one exited with status 0 (was successful).;
runs both the commands, even if the first one exits with a non zero status.Your example with
&&
can be equivalently paraphrased asUsing
;
will execute the commands irrespective whether first command is successful or not.Using
&&
will execute the second command only when first command executed successfully (status 0).Both are used on different perspective. Like for a longer process, say for an installation you need to compile and install it. you should
make && make install
. So the install will run only ifmake
successful.So for dependent commands you should use
&&
.Wring bash, or commands with independent commands, use
;
.So if you want to shutdown computer even the first job failed use
;
, but if want on complete success of first job initiate the shutdown use&&
.a ; b
will run b regardless of the exit status of a.a && b
will run b only if a succeeded.This is necessary and sufficient to answer to the first 3 questions. In particular, the 2 is too broad, and cannot be given "one" definitive answer - your best bet is to decide on a case by case basis.
As for the 4th question: They're Bash syntax.
There is no intrinsic danger in using either. Again, the definition above is sufficient. It implies that you will write
&&
whenb
has unintended effects ifa
does not succeed. There is no need for further rules or explanation, IMHO.Jack's answer is very good rule of thumb.
I would add that in some cases, using these commands in a subshell make sense when we want to consider them as a single unit or we don't want to couple some operations outcomes with the current shell.
Examples:
Concatenate the output of two commands:
Go into a directory and perform a command from there without changing the current directory of the shell:
Kudos to Jack's cheatsheet for saying how this works clearly and succinctly. But all of these things have a more general reason for being chosen. You don't just have to memorize these facts for when you're doing stuff in the shell. For more context you might also like to know that:
;
is commonly used as a statement separator in languages like C, C++, Perl, Java, C#, awk, and a slew of languages that have been derived from those. It performs the same function in all of those: do one thing, then the next. It is like a period at the end of a sentence.&
is used in C for bitwise AND so&&
was used for logical AND. Logically ANDing things in C includes a feature called shortcutting. Shortcutting means that for an AND that if the first thing is false there's no reason to check the second thing. A single false item in an AND means the whole thing will be false. In the shell that means it won't go onto the second command if the first one fails. Or if the first thing worked, it will go onto the second thing to see if it "might be false".|
is used in C for bitwise OR so||
is used for logical OR. Logical OR's also feature shortcutting. In an OR this means that you look for the first true thing. So if the first thing fails it will try the second thing.Others have said it well. From a practical perspective, I often find that if you want to chain commands then you would like to know where something fails as well. For this you need to understand grouping in bash commands using the curly brackets.
Instead of
a && b
you will want to write: