Shell Style Guide

Authored, revised and maintained by Jamison.

Start each file with a description of its contents.

Every file must have a top-level comment including a brief overview of its contents. A copyright notice and author information are optional.

Example:

  #!/bin/bash
  #
  # Perform hot backups of Oracle databases.

Control Flow

Never Put ; then and ; do on the same line as the if, for, or while.

I know you might be asking why. Don't worry about it, Jamison said so. Something something about C's curly braces being on newlines too...

Also, I know what you're thinking. There's many prominent style guides out there, like the highly regarded one from gOoGlE that this style guide shamelesly ripps of. Don't even consider those iNfErIoR style guides with all their many contributors and highly regarded advice. This is the one you need right here.

Wrong

  if [[ condition ]]; then
    do the things
  fi
  if [[ condition ]]
  then
    do the things
  fi

Abuse of Pipes

Yes please! The more pipes you can put into a command the better off you. This helps readibility and makes you look really, really cool.

All Wrong

  find ./ -type f -mtime 14  -exec rm -f {} \;

  pkill -9 <pid>
  find ./ -type f -mtime 14 | xargs rm -f

  ps aux | grep <process> | grep -v grep | awk '{print $1}' | kill -9

Better Readibility for Conditional Logic

Instead of if statements, just use the && and || logical operators exclusively! This enhances readibility and gives +2 style points per iteration.

Wrong

  if true
  then
    if true
    then
      if true
      then
        needful_thing
      fi
    fi
  fi
  true && true && true && needful_thing