summaryrefslogtreecommitdiff
path: root/STYLE
blob: e99d955ce0ef98a8dfaf621f78508cfdc10cf8a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
This is the rc-scripts style manual.  It governs the coding style 
of rc-scripts.  Everything here might as well have been spoken by 
God.  If you find any issues, please talk to base-system@gentoo.org 
or #gentoo-base on irc.freenode.net.

#############
# VARIABLES #
#############
- User Variables -
    Variables must always be enclosed by {}
      e.g. ${foo} ${bar}
- Internal Bash Variables -
    Do not use {} with internal variables unless appropriate
      e.g. case $1 in
      e.g. foo=$IFS
      e.g. echo "blah${1}123"
- Assigning with Quotes -
    When assigning to a variable from another variable or 
    a subshell, you do not need quotes, bash handles it
      e.g. foo=${bar}
      e.g. foo=$(uname -a)

#########
# TESTS #
#########
- Brackets -
    Always use the [[ ... ]] form instead of [ ... ]
- Equality -
    Always use == form instead of = when testing equality.  Makes 
    for saner looking code (we all love C, don't lie).
- Quoting -
    When dealing with strings, do not quote the LHS variable.
    The [[ ... ]] handles spaces properly in variables.  You 
    should always quote strings though.
      e.g. [[ ${foo} = "bar" ]]
      e.g. [[ "foo" != "bar" ]]
    The RHS variable needs to be quoted to prevent against accidental 
    pattern matching (unless you want to match, of course).
      e.g. [[ ${foo} = "${bar}" ]]
    If foo=a-b and bar=a-* and ${bar} was not quoted, you would 
    get a match!

###############
# CODE BLOCKS #
###############
- Structure -
    Use the more compact form
      e.g. if ... ; then
      e.g. while ... ; do
    Do not use the older form
      e.g. if ...
           then
- Functions -
    Use the more compact form
      e.g. foo() {
    Do not lead with 'function '
      e.g. function foo() {

############
# COMMENTS #
############
- General -
    Try to include a comment block before sections 
    of code to explain what you're attempting