An aside about shell conditionals

The && shell operator is used in two common contexts. First, to chain commands like ; does, but stop on failure. For example:

$ mkdir foo && cd foo && echo ‘done!’ && cd ..
done!
$ mkdir foo && cd foo && echo ‘done!’ && cd ..
mkdir: foo: File exists

The directory existed on the second try, so mkdir set $?, the error code, to 1. An error code of 0 is success; non-zero is failure. The && operator saw that $? was non-zero, meaning that the mkdir failed, so it exited.

Read more about this article at: http://blog.extracheese.org/2010/11/an-aside-about-shell-conditionals.html

Leave a Reply

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