Skip to content

Guideline for aliases

linux-shell-base edited this page Oct 18, 2017 · 16 revisions

Guideline for aliases and short functions in Linux-shell-base.

  1. General
  2. Format
  3. Code style

General

An alias/short function ...

  • is categorized.
  • has a description.

Format

Alias and short function files are created in the following format:

alias name='alias start...                          ...alias end'                # name: description
alias name='alias start...                                         ...alias end' # name: description
alias name='alias start...                                                             ...alias end' # name: description

name() { function line 1 start...                       ...function line 1 end \
  function line 2 start...                             ...function line 2 end; } # name: description

name() { function line 1 start...                   ...function line 1 end \
  function line 2 start...                              ...function line 2 end \
  function line 3 start...    ...function line 3 end; }                          # name: description

name() { function line 1 start...                   ...function line 1 end \
  function line 2 start...                      ...function line 2 end \
  function line 3 start...                  ...function line 3 end; }            # name: description

name() { function start...                          ...function end; }           # name: description
name() { function start...                  ...function end; }                   # name: description
name() { function start...                                ...function end; }     # name: description
  • A line is a maximum width of 100 characters for aliases and 80 character for short functions (discluding the description). If an alias exceeds the 100 character limit, it is created as a short fuction instead.
  • If a short function is more than 3 lines, it should be changed to a script.
  • The comment specifier ("#") starts on column 82. If an alias line is wider than 80 characters, the comment specifier starts one space after the end of the alias.
  • A line is continued on the next line only with the last (space separated) word that exceeds the 80 character limit.
  • Short functions with more than one line are separated by a blank line.
  • A description is all lowercase letters (except for names and acronymns that need to be capitalized or uppercase).

For an example, see aliases-main.bash.

Code style

General

Lines and indentation
  • Spaces are used for all whitspace.
Variables
  • A variable name uses acronyms whenever possible.
Strings
  • Single quotes are used when possible.

Bash

Variables
  • A non-global variable inside a function is declared local.
  • A variable in an arithmetic expression is called without brackets (e.g. $((numEntries - count)), not $((${numEntries} - ${count}))).
  • An argument variable (e.g. ${1}) is used directly rather than being assigned to a new variable when possible.
Functions
  • A function name uses camel casing.
Conditionals
  • A single bracket conditional is used when possible.
Output and redirection
  • An error message is redirected to stderr with 1>&2.
Other
  • A parameter expansion or command substitution is quoted unless otherwise required.
  • A non-zero exit code is returned when exiting due to an error.

All other code style rules are up to the user. Contributions will be reviewed.

Clone this wiki locally