General
In general, most of C coding convention apply to Bash coding convention, for example:
- - Indentation is always one shift-width(4):
- - case has the same indentatio level and switch
- - ...
Function parameters
Function parameters should define local their names, not use $1, $2...
returning a string value:
RS="the value"
returning an array value:
RA=(the values...)
returning multiple string/array values
returning multiple string/array values use RS_{name}
or
RA_{name}:
argv handling and usage()
argv handling and usage(): use shift to process, and always have usage() function:
Where integer operations are relevant
Where integer operations are relevant, use ((C_EXPRESSION)) instead of [SHELL_EXPRESSION]:
SHELL EXPRESSIONS
In SHELL EXPRESSIONS use [[ ]]
instead of [ ]
when AND/OR is needed:
When the script is included in other scripts
When the script is included in other scripts, use the following method to avoid multiple includes:
Quotation:
the sign $@
is a special sign for all the parameters passed to
the function.
when quoting it "$@"
bash will quote each parameter seperatly.
if you want to use all the parameters, you should use an array:
cmd=("$@")
cmd=("${cmd[@]}" "some other text")
"${cmd[@]}"
The above three lines, save the parameters into an array, adds to it
another
value and then us it as a bash command.
Quotation should always take under consideration when passing vars and
arrays to other functions or when excecuted as a command.