Deleting files
Never do:
rm -rf $files_to_delete
Never, ever, ever do:
rm -rf /$files_to_delete
Use this instead:
[ -n "$files_to_delete" ] && rm -rf $files_to_delete
Best practices
- Initialize your variables, especially if they are numbers and you want to use them in printf statements.
- Put quotes around your arguments if they could ever be null, and especially when used in printf statements.
- Make sure you have the correct number of arguments, especially considering what the line will look like after the shell substitutions have occurred.
- The safest way to display an arbitrary string is to use
printf '%s\n' "$string"
.