What is the difference between the following methods of chaining commands?
cmd1; cmd2
cmd1 && cmd2
|
|
If you have
|
|||||||||||||
|
|
The first line will execute one command after another, irrespective of whether the first one succeeded or not. The second line is an example of shell logic: it will only execute the second command if the first one succeeded. This is because |
|||
|
|
|
A command returns 0 to indicate success, and a nonzero error code (between 1 and 255, usually between 1 and 125 as higher values have other meanings) to indicate failure. Thus You can tell the shell to enter “exit on error” mode by running ¹ This isn't to say you can blindly replace |
|||||||||||
|
|
More practical, there's a difference between
and
. Besides it's a stupid way, the first one will remove your whole filesystem (or $HOME, depending how it's called) because it runs |
|||||||
|