trap in Bash: Clean Up When Your Script Dies
Use trap EXIT to clean up temp files, INT/TERM for graceful shutdown, and ERR for errors. Stop your bash scripts from leaving messes when they die.
All the articles with the tag "scripting".
Use trap EXIT to clean up temp files, INT/TERM for graceful shutdown, and ERR for errors. Stop your bash scripts from leaving messes when they die.
set -x prints every command before it runs. Add PS4 for context. Use trap for cleanup. Here's the toolkit every bash debugger needs.
Unquoted bash variables split on IFS and quietly break loops, file ops, and filenames with spaces. Here's why it happens and how to always quote right.
Bash has built-in string operations: substring extraction, find-replace, case conversion, and trimming. You don't need sed for basic text work.
set -e looks bulletproof, but it slips right through pipes, command substitution, and conditionals. The bash gotchas that bite at 2 AM, and how to fix them.
Bash does arithmetic natively, so you can drop bc for most math. Arithmetic expansion, conditions, bitwise operators, and the trap inside $(( )).
set -euo pipefail makes your bash scripts fail fast instead of silently. Here's what each flag does and why they matter.
awk is perfect for parsing logs. 5 patterns for filtering, summing, counting, splitting fields, and reformatting, no Perl or Python required.
jq is JSON on the command line. Here are 5 one-liners that actually solve real problems: filtering, extracting, transforming, combining, and debugging.
xargs and while read both loop over input, but they handle arguments, signals, and performance differently. Here's when to use each.
Practical find command examples: -mtime, -newer, -type, -exec vs xargs, -not, size filters. The patterns you google every time.
Write bash scripts that don't silently fail, set -euo pipefail, error handling, input validation, and logging patterns for production scripts.