← All posts

Five small Bash habits that save me every week

8 March 2022 · 1 min read
#bash#linux

None of these are clever. They just stop small mistakes from becoming big ones.

  1. Start every script with set -euo pipefail. Stop on the first error, treat unset variables as bugs, and don't hide failures inside pipes.
  2. Quote your variables. "$file", not $file. One filename with a space in it will teach you this the hard way.
  3. Print before you delete. Run the find on its own first, read the list, then add -delete.
  4. Use trap to clean up. Temporary files get removed even when the script fails halfway.
  5. Log with timestamps. A one-line log() function that prints the time makes every script easier to debug later.

Bash is great glue. When a script grows past a page or needs real logic, I move it to Python.

T
Written by Teza Wynn Htut

Cloud and infrastructure engineer in Auckland. I write down the fixes and lessons from things I build, and make tutorial videos on YouTube.

Watch on YouTube