Defensive File Operations
Backup Name
Plan Before Writing
Before overwriting a file, a script can compute the backup name and report the copy plan. The visible plan is easier to review than a hidden write.
Program
Play the script to choose the filename and see the backup plan.
backup_name_plan.sh
#!/usr/bin/env bash
filename=
backup="$filename.bak"
plan="copy $filename $backup"
echo "$plan"
#!/usr/bin/env bash
filename=
backup="$filename.bak"
plan="copy $filename $backup"
echo "$plan"
backup
A backup preserves the old file before a risky write.
plan
Printing the plan makes the operation reviewable before execution.
suffix
A predictable suffix keeps backup names easy to find.