-
Use four spaces for indentation. No tabs.
-
Shell/local variables and function names follow "snake_case" - only lowercase letters, underscores, and digits.
-
Environment variables use all uppercase names. For example, BASE_HOME, BASE_HOST, BASE_OS, BASE_SOURCES. See: https://stackoverflow.com/a/42290320/6862601
-
In rare cases of global variables being shared between library functions and their callers, use all uppercase names. For example: OUTPUT and OUTPUT_ARRAY
-
Place most code inside functions and invoke the main function at the bottom of the script.
-
In libraries, have top level code that prevents the file from being sourced more than once. For example:
[[ $__stdlib_sourced__ ]] && return __stdlib_sourced__=1
-
Make sure all local variables inside functions are declared local.
-
Use func naming convention for special purpose variables and functions. Use a leading underscore for "private" variables and functions.
-
Double quote all variable expansions, except:
- inside [[ ]] or (( ))
- places where we need word splitting to take place
-
Use [[ $var ]] to check if var has non-zero length, instead of [[ -n $var ]]. See: https://stackoverflow.com/a/49825114/6862601
-
Use "compact" style for if statements and loops:
if condition; then ... fi while condition; do ... done for ((i=0; i < limit; i++)); do ... done
-
Make sure the code passes https://shellcheck.net checks.