Puredyne/Coding Style
Appearance
< Puredyne
Shell Scripting
[edit | edit source]This guideline is heavily inspired from the Debian Live Coding Style.
Compatibility
[edit | edit source]- Don't use syntax or semantics that are unique to the Bash shell. For example, the use of array constructs.
- Only use the POSIX subset - for example, use
$(foo)
over`foo`
. - You can check your scripts with 'sh -n' and 'checkbashisms'
Indenting
[edit | edit source]- Always use tabs over spaces.
Wrapping
[edit | edit source]- Generally, lines are 80 chars at maximum.
- Use the "Linux style" of line breaks:
# Bad:
if foo; then
bar
fi
# Good:
if foo
then
bar
fi
- The same holds for functions:
#Bad:
foo () {
bar
}
#Good:
foo ()
{
bar
}
Variables
[edit | edit source]- Variables are always in capital letters.
- Use braces around variables; eg. write
${FOO}
instead of$FOO
. - Always protect variables with respect to potential whitespaces, write
"${FOO}"
not${FOO}
. - For consistency reasons, always use quotes when assigning values to variables:
#Bad:
FOO=bar
#Good:
FOO="bar"
- If multiple variables are used, quote the full expression:
#Bad:
if [ -f "${FOO}"/foo/"${BAR}"/bar ]
then
foobar
fi
#Good:
if [ -f "${FOO}/foo/${BAR}/bar" ]
then
foobar
fi
Miscellaneous
[edit | edit source]- Use
|
as a separator in calls to sed, e.g.sed -e 's|foo|bar|'
. - Don't use the test command for comparisons or tests, use
[ ]
, e.g.if [ -x /bin/foo ]; ...
and notif test -x /bin/foo; ...
.