Bash Script Sunday #2: Writing a Safe and Robust Bash Script
Bash Script Sunday #2: Writing a Safe and Robust Bash Script⌗
In today’s post, I’d like to give some insight into writing safer scripts using Bash’s built-in options.
Why Script Safety Matters⌗
A poorly written script can cause unintended data loss, infinite loops, or security vulnerabilities. Writing robust scripts ensures they behave predictably and handle errors gracefully.
1. Enabling Safe Bash Options⌗
Bash provides built-in options to catch errors early and prevent common pitfalls.
Add this at the start of your script:
Breakdown of these options:⌗
-e
: Exit immediately if any command fails.-u
: Treat unset variables as errors.-o pipefail
: Return the exit status of the first failing command in a pipeline.
Example: Handling unset variables safely⌗
Running this script without defining $filename
will trigger an error instead of running with unintended behaviour.
2. Input Validation⌗
Never assume user input is correct. Use read
with validation checks.
3. Preventing Dangerous Expansions⌗
If handling filenames, always quote variables to prevent issues with spaces or special characters.
Bad example:
If $dir
is empty, this expands to rm -rf /*
- disastrous!
Safe version:
4. Using Temporary Files Safely⌗
Use mktemp
to avoid collisions instead of relying on predictable filenames.
5. Handling Errors Gracefully⌗
Use trap
to clean up resources on failure.
Conclusion⌗
By following these principles, you can make your scripts safer, more reliable, and easier to maintain. Always test your scripts in a safe environment before using them in production.