Dec9th's Opslog

Google Shell Style Guide

Environment

STDOUT vs STDERR

err() {
  echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2
}

if ! do_something; then
  err "Unable to do_something"
  exit 1
fi

Formatting

Indentation

Line Length and Long Strings

Pipelines

# All fits on one line
command1 | command2

# Long commands
command1 \
  | command2 \
  | command3 \
  | command4

Loops

Case statement

  case "${flag}" in
    a) aflag='true' ;;
    b) bflag='true' ;;
    f) files="${OPTARG}" ;;
    v) verbose='true' ;;
    *) error "Unexpected option ${flag}" ;;
  esac

Naming Conventions

Function Names

Variable Names

for zone in "${zones[@]}"; do
  something_with "${zone}"
done

Constants and Environment Variable Names

Source Filenames

Read-only Variables

Use Local Variables

Function Location

Main

Calling Commands

Checking Return Values

Builtin Commands vs. External Commands


마무리

종종 찾아보는 google shell style guide 신규로 작성할 때 마다 다시 보고 작성함. ㅠㅠ 외워지지가 않네

Reference