Author: | Garrett Cooper |
---|---|
Date: | 2012-01-13 |
Revision: | 9519 |
Copyright: | Shell Style Guide |
The following guide will go over the shell coding standard for the FreeNAS project.
Style must be consistent with the surrounding code, in particular if it's contributed via a third party. This requirement has been established to be consistent with style(9)'s requirements in FreeBSD -- despite the fact that style(9) applies solely to C code.
Using set -e and set -u will help the developer find potential bugs in his/her code related to bad exit codes and unset variables. If set -e is used in tandem with set -u, sh will stop whenever a variable is unset.
Conditionals should be composed in this format:
if : then # This will always be executed fi while : do # Do something here : done for : do # Do something here : done
If the scope of a variable (even a loop variable) is local to the function, one should always be declared local.
hereto-docs are constructs that allow the author to compose multiline inputs to various commands (e.g. cat). An example follows:
cat <<EOF >a-file this is a multiline message EOF
The FreeBSD project as well as many other projects compose hereto docs in the above format. Furthermore, using the above format improves readability in emacs, vim, etc, in particular when things are colorized.