Skip to content

Bash Shell Scripting CodingStyle

Techlive Zheng edited this page Jan 5, 2014 · 3 revisions

Shell scripting is very flexible, some basic coding standards should be followed.

  1. Variable naming convention

    Environmental variable name should be in upper case, any other variable name should all be in lower case. Global variable name should be prefixed with __ (eg, $__file), but local variable name should not have any prefix.

  2. Function naming convention

    Function name should be prefixed with _ (eg, _current_path) in lower case.

  3. Using $(..) instead of \..`` for command substitution

  4. Double quote any command parameter

  5. Keep if statement as follow

     if ...
     then
         ...
     elif ...
     then
         ...
     else
         ...
     fi
    

    Do not write if and then in the same line

  6. Use extended test [[]] instead of [] as much as possible

  7. Use OPT_* for internal option holder

Clone this wiki locally