So, I deleted my home folder (or, more precisely, all files I had write access to). What happened is that I had
build="build"
...
rm -rf "${build}/"*
...
<do other things with $build>
in a bash script and, after no longer needing $build
, removing the declaration and all its usages -- but the rm
. Bash happily expands to rm -rf /*
. Yea.
I felt stupid, installed the backup, redid the work I lost. Trying to move past the shame.
Now, I wonder: what are techniques to write bash scripts so that such mistakes can't happen, or are at least less likely? For instance, had I written
FileUtils.rm_rf("#{build}/*")
in a Ruby script, the interpreter would have complained about build
not being declared, so there the language protects me.
What I have considered in bash, besides corraling rm
(which, as many answers in related questions mention, is not unproblematic):
rm -rf "./${build}/"*
That would have killed my current work (a Git repo) but nothing else.- A variant/parameterization of
rm
that requires interaction when acting outside of the current directory. (Could not find any.) Similar effect.
Is that it, or are there other ways to write bash scripts that are "robust" in this sense?
rm -rf "${build}/*
no matter where the quote marks go.rm -rf "${build}
will do the same thing because of thef
.set -u
isn't nearly as frowned on asset -e
is, but it still does have its gotchas.#! /usr/bin/env ruby
at the top of every shell script and forget about bash ;)