This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
tools:style_guides:bash [2018/09/07 14:44] pechevar |
tools:style_guides:bash [2018/09/07 15:14] (current) pechevar |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | These are basic style recommendations, | + | These are basic style recommendations, |
+ | | ||
In order to achieve more efficient and homogeneous coding, it's been agreed that new codes submitted to the different git projects would be accepted to be committed to the master branches only if the style guide recommendations are respected. This is also a strong recommendation for your personal scripts. | In order to achieve more efficient and homogeneous coding, it's been agreed that new codes submitted to the different git projects would be accepted to be committed to the master branches only if the style guide recommendations are respected. This is also a strong recommendation for your personal scripts. | ||
- | * File Header | + | ====== |
The first line of your file must indicate in which language your script is written (http:// | The first line of your file must indicate in which language your script is written (http:// | ||
Every file must have a top-level comment including a brief overview of its contents and eventually how to use it. Author information is recommended. | Every file must have a top-level comment including a brief overview of its contents and eventually how to use it. Author information is recommended. | ||
Line 24: | Line 27: | ||
# Adapted: Pierre-Antoine Bretonnière - IC3 , January 2013 | # Adapted: Pierre-Antoine Bretonnière - IC3 , January 2013 | ||
</ | </ | ||
- | * Indentation | + | ====== |
Indent 2 spaces. NO TABS. | Indent 2 spaces. NO TABS. | ||
Use blank lines between blocks to improve readability. Indentation is two spaces. Whatever you do, don't use tabs. For existing files, stay faithful to the existing indentation. | Use blank lines between blocks to improve readability. Indentation is two spaces. Whatever you do, don't use tabs. For existing files, stay faithful to the existing indentation. | ||
- | * Loops | + | ====== |
Put ; do and ; then on the same line as the while, for or if. | Put ; do and ; then on the same line as the while, for or if. | ||
Line 36: | Line 39: | ||
Example: | Example: | ||
- | < | + | < |
if [[-d_dir_: | if [[-d_dir_: | ||
log_date " | log_date " | ||
Line 51: | Line 54: | ||
done | done | ||
</ | </ | ||
- | * Case statement | + | ====== |
Indent alternatives by 2 spaces. | Indent alternatives by 2 spaces. | ||
Line 60: | Line 63: | ||
Example: | Example: | ||
- | < | + | < |
a) | a) | ||
variable=" | variable=" | ||
Line 75: | Line 78: | ||
Simple commands may be put on the same line as the pattern and ;; as long as the expression remains readable. | Simple commands may be put on the same line as the pattern and ;; as long as the expression remains readable. | ||
- | * Command Substitution | + | ====== |
Use $(command) instead of backticks. | Use $(command) instead of backticks. | ||
Line 82: | Line 85: | ||
Example: | Example: | ||
- | < | + | < |
var=" | var=" | ||
# This is not: | # This is not: | ||
var=" | var=" | ||
- | </ | + | </ |
- | | + | |
+ | ====== | ||
< | < | ||
< | < | ||
Example: | Example: | ||
- | < | + | < |
# alnum character class followed by the string name. | # alnum character class followed by the string name. | ||
# Note that the RHS should not be quoted here. | # Note that the RHS should not be quoted here. | ||
Line 113: | Line 117: | ||
fi | fi | ||
</ | </ | ||
- | * Function and variables names | + | ====== |
Lower-case, with underscores to separate words. Parentheses are required after the function name. The keyword function is optional, but must be used consistently throughout a project. | Lower-case, with underscores to separate words. Parentheses are required after the function name. The keyword function is optional, but must be used consistently throughout a project. | ||
Same thing for variables: lower-case, with underscores to separate words, with names as explicit as possible. | Same thing for variables: lower-case, with underscores to separate words, with names as explicit as possible. | ||
However, constant (paths, environment variables and parameters passed through namelists) must be all caps, separated with underscores, | However, constant (paths, environment variables and parameters passed through namelists) must be all caps, separated with underscores, | ||
- | * Arithmetic | + | ====== |
- | Never use the let command, nor use the $[...] syntax, nor use the shell helper expr. Instead, use $((...)) to perform all math operations in conjunction with the colon null utility. | + | Never use the let command, nor use the '' |
- | When using variables, avoid using the ${var} form when possible. | + | When using variables, avoid using the '' |
Example | Example | ||
- | < | + | < |
# as it is not as portable, and this version isn't much longer. | # as it is not as portable, and this version isn't much longer. | ||
# Note that: | # Note that: | ||
Line 140: | Line 144: | ||
echo $(( (min * 60) + sec )) | echo $(( (min * 60) + sec )) | ||
</ | </ | ||
- | * Pipelines | + | ====== |
Pipelines (http:// | Pipelines (http:// | ||
Line 149: | Line 153: | ||
Example: | Example: | ||
- | < | + | < |
+ | # All fits on one line | ||
command1 | command2 | command1 | command2 | ||
# Long commands | # Long commands | ||
- | command1 \ | + | |
| command2 \ | | command2 \ | ||
| command3 \ | | command3 \ | ||
- | </ | + | | command4 |
+ | </ | ||