Dedicated fd for structured logging

Open fd 3 to a file, write to it independently of stdout, then close it

exec 3>>audit.log; echo "$(date -Is) start" >&3; exec 3>&-

tool
bash12 commands
tags
bashobservability

More bash commands

  • setopt extendedglob; print -l **/*(.); autoload zmv; zmv '(*).txt' '$1.bak'

    zsh: list only regular files recursively, then mass-rename safely with zmv

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

    Fail fast on errors, unset vars, pipe failures; split only on newline/tab

  • tmp=$(mktemp -d); trap 'rm -rf "$tmp"' EXIT INT TERM

    Auto-remove mktemp dir on exit/signal; note: quote $tmp to survive spaces

  • diff <(kubectl get cm a -o yaml) <(kubectl get cm b -o yaml)

    Process substitution compares two streams without temp files

  • mapfile -t lines < file.txt; printf '%s\n' "${lines[@]}"

    mapfile -t strips newlines; handles spaces unlike for-loop word splitting

all 12 bash commands