-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbash_style.txt
42 lines (35 loc) · 1.49 KB
/
bash_style.txt
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
2010-07-22 Jeff McCune <[email protected]>
Bash style suggestions:
= Cleanup actions =
May be done with trap {} EXIT
# JJM Note the use of a string literal.
# The variable will be dereferenced when the trap fires.
# "Idiom - If $master_pid length is nonzero, then kill $master_pid"
trap '{ test -n "${master_pid:-}" && kill "${master_pid}" ; }' EXIT
= Functions =
Functions should be declared like so:
somefunction() {
local myvar=${1:-mydefault}
}
= Shell Substitution and Variables =
* Please don't use let or declare to set variables.
* Local variables should be lower case.
* Global variables should be upper case.
* Use of global variables in functions should be avoided.
* :- and := operators may be used to return or set defaults.
e.g. echo ${FOO:=default} and echo ${FOO:-default}
* Basic string slicing may be done with ## and %%:
foo=hello/world; echo "${foo%%/*} ${foo##*/}" prints
"hello world"
* basename "$0" will return the script without path
The idiom is PROGNAME=$(basename "$0")
* $() should be preferred to `` for readability
* <<'EOF' for a literal here document, <<EOF for an interpolated one
= General Patterns =
Acceptance tests should exit $EXIT_FAILURE or $EXIT_OK if the reach
a valid conclusion about the state of puppet. All other exit codes
should be considered as an error in the spec test itself.
It's a good idea to run with set -u to catch undefined variables being
used. Where possible, set -e is also a good idea.
= Bash Pitfalls =
http://mywiki.wooledge.org/BashPitfalls