This is a compilation of a common patterns I use in my bash scripts:

Headline

#!/usr/bin/env bash

set -euo pipefail
IFS=$'\n\t'

Logging

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

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

Confirmation prompt

echo -n "Do something dangerous? [y/n]"
read -r ans
if [[ $ans != "y" ]]; then
  exit 0
fi

Positional argument

ARG=${1:-}
if [[ -z ${ARG} ]]; then
  echo "usage: $0 [ARG]"
  exit 1
fi

Random number

readonly RANDOM_PORT=$(shuf -i 1024-49151 -n 1)

Check software availability

if ! command -v aws>/dev/null; then
  echo "AWS CLI is missing..."
fi

Redirect stderr to stdout

0 */2 * * * /usr/local/bin/backup >> /var/log/cron.log 2>&1