bash commands

12 commands, each with a one-line explanation. Click a command to copy it, or its title for details.

Commands 12 shown

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

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

    troubleshooting · ci

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

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

    security · troubleshooting

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

    Process substitution compares two streams without temp files

    debug · k8s

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

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

    troubleshooting

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

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

    performance

  • [[ $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

    debug

  • find . -name '*.log' -print0 | xargs -0 -P"$(nproc)" -n1 gzip

    Run N parallel jobs safely with NUL separators for names with spaces

    performance

  • curl -s "$url" | jq .data; echo "${PIPESTATUS[@]}"

    Inspect exit code of every pipe stage; $? only reports the last command

    debug · troubleshooting

  • while getopts "n:v" o; do case $o in n) ns=$OPTARG;; v) verbose=1;; esac; done

    POSIX option parsing with arguments; -n takes a value, -v is a switch

    ci

  • timeout -k 5s 30s ./long_task.sh || echo "timed out: $?"

    SIGTERM at 30s then SIGKILL after 5s grace; exit 124 means it was killed

    troubleshooting

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

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

    observability

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

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

    zsh · troubleshooting