Strict mode with safe IFS

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

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

tool
bash12 commands
tags
bashtroubleshootingci

More bash commands

  • 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

  • f=/var/log/app.log.gz; echo "${f##*/}" "${f%.*}" "${f//log/LOG}"

    basename, strip extension, and global replace without spawning sed/basename

  • [[ $s =~ ^([0-9]+)\.([0-9]+) ]] && echo "${BASH_REMATCH[1]}/${BASH_REMATCH[2]}"

    Native regex match extracts groups; note: don't quote the right-hand regex

all 12 bash commands