Bash Scripting Sunday #5: Safely Working with Temporary Files in Bash
Learn best practices for safely creating and cleaning up temporary files in your Bash scripts using mktemp, traps, and secure patterns.
Bash Scripting Sunday #4: Creating a Simple Interactive Menu in Bash
Learn how to create interactive, user-friendly command-line menus using select, PS3, and simple loops in Bash.
Bash Scripting Sunday #3: Using xargs Effectively – More Than Just a Pipe
Learn how to unlock the power of xargs in Bash to build fast, flexible command pipelines without forking too many subshells.
Bash Scripting Sunday #2: Writing a Safe and Robust Bash Script
Bash Scripting 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.
Bash Scripting Sunday #1: Bash Parameter Expansion - Save Time and Avoid Subshells
Bash Scripting Sunday #1: Bash Parameter Expansion - Save Time and Avoid Subshells
In this entry, I’d like to show you how to use Bash Parameter Expansion to make your life a little easier, your scripts a little quicker, and use less resources.
Here’s an example script that isn’t using Bash Parameter Expansion:
#!/usr/bin/env bash
filename="/path/to/file.txt"
echo "Basename: $(basename $filename)" # file.txt
echo "Dirname: $(dirname $filename)" # /path/to
This script will output: