There is plenty of code that does not adhere to these conventions currently. However it is useful to have conventions as consistently formatted code is easier to read and less likely to hide bugs. New code should adhere to these conventions, and developers should consider sensible adjustment of existing code when working nearby.
Please follow conventions described in OpenStack style guidelines [1] and Bashate [2].
As well as those rules described in Bashate [2]:
The interpreter is /bin/bash.
Provide a shebang #!/bin/bash if you intend your script to be run rather than sourced.
Use set -e and set -o pipefail to exit early on errors.
Use set -u to catch typos in variable names.
Use $() not `` for subshell commands.
Double quote substitutions by default. It’s OK to omit quotes if it’s important that the result be multiple words. EG given VAR=”a b”:
- echo "${VAR}"
Quote variables.
- echo "$(echo a b)"
Quote subshells.
- echo "$(echo "${VAR}")"
In subshells, the inner quotes must not be escaped.
- function print_b() { echo "$2"; }; print_b ${VAR}
You must omit quotes for a variable to be passed as multiple arguments.
- ARRAY=(${VAR}); echo "${#ARRAY[@]}" = 2
You must omit quotes to form a multi-element array.
Avoid repeated/copy-pasted code. Make it a function, or a shared script, etc.
[1] | (1, 2) http://docs.openstack.org/developer/hacking/ |
[2] | (1, 2, 3) http://git.openstack.org/cgit/openstack-dev/bashate/tree/README.rst |