Log to a separate file descriptor

Open fd 3 to a file, write to it separately from 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 safely rename many files with zmv

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

    Stop on errors, unset variables and failed pipes. Split words only on newline and tab

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

    Remove the mktemp dir on exit or signal. Note: quote $tmp so paths with spaces work

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

    Process substitution compares two outputs without temp files

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

    mapfile -t strips newlines and keeps spaces, unlike word splitting in a for loop

all 12 bash commands