Today’s article will be short and will cover a bash topic that frustrates me to no end. Please don’t use
; to separate commands, when you mean
&&. There is an important difference between the two and many developers never realize that they want to be using
&& in their scripts.
Here is a common oneliner that you might use to compile a package:
./configure ; make ; make install
In this example, we intend to configure the build, make it, and then install it (in that order). There is no reason to run
make if the configuration step failed, and no reason to run
make install, if either of the previous steps failed. However, that is exactly what the above code will do.
What you really mean to write is:
./configure && make && make install
Now each command will only execute if the previous command was successful.
; operator is used as a separator between commands, bash will execute each command in order, but it doesn’t care about the result of the previous command. When the
&& operator is used as a separator between commands, bash will execute each command in order, but will continue only when the previous command return zero (errors are non-zero). For that reason, anytime command
a is expected to execute successfully before running command
&& should be used instead of
The reason that this gets under my skin, is that almost every time I see commands chained like this, the developer intended that command
a executed sucessfully, before running command
b, and every now and then something really bad happens if
b runs and
a did not. Please be conscientious of your fellow developers, who may have to consume your scripts, and make sure they do what you intend by using
There is another separation operator
||, which will execute each command in order, but will continue only when the previous command returns a non-zero value. I like to use this operator with testing commands, where command
a will fail and now command
b needs to be run to fix things, or maybe command
a was a platform test and command
b needs to be run on other platforms.