Use && Instead of Semicolon to Separate Commands

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.

How do it…

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.

How it works…

When the ; 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 b, then && 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’s more…

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.

Bash Profile Tips & Tricks & Conditional Sourcing

One trick I like to use, since I develop on multiple computers, is to define my .bash_profile in a shared place (such as Dropbox), and setup a symlink to it on the each of my computers. This works great, even when I have slightly different configurations for each computer, because my configuration only lives in one place and I conditionally target each class of computer. Today’s article describes some of my tips and ...