Don’t delete production
Learn the habits that stop one bad command from ruining your week.
- Preview destructive commands before running them
- Explain how an empty variable or a stray space becomes a disaster
- Review scripts with ShellCheck, and judge curl | bash
The shell does exactly what you tell it, right now, with no undo. Most famous shell disasters come down to a few small mistakes, and a few habits stop nearly all of them.
The one-character disaster
Here’s a line from a real kind of cleanup script. What could go wrong?
rm -rf "$BUILD_DIR"/*If BUILD_DIR is empty or unset, Bash happily runs rm -rf /*. A typo in a variable name does it too, because Bash treats unknown variables as empty. Two defenses:
${BUILD_DIR:?}stops the script with an error if the variable is empty.set -umakes any unset variable an error.
And a stray space is just as bad: rm -rf / tmp/build deletes / and tmp/build.
Try it
Up to 15 XPNear miss at the airlock
You’re cleaning up the station’s temp folder. Everything here is simulated, so make your mistakes now.
Mission 1 of 4
See the danger
Hopper: “Set TARGET=cache, then preview your delete by putting echo in front: echo rm -r $TARGET/*”
Simulated shell. Try ls, or type help to see the commands.
- cache
- a.tmp
- b.tmp
- my
- my notes.txt
- precious
- mission-data.db
- See the danger
- Meet the typo
- Add the guard
- Quote the name
Habits that save you
- Preview: put
echoin front, or runlswith the same pattern first. - Quote every variable:
"$file". - Use
set -euo pipefailin scripts, and--before file names that might start with a dash. - Check where you are:
cd /some/dir && rm -r *won’t delete anything if thecdfails, butcd /some/dir; rm -r *deletes from wherever you were. - Run ShellCheck on every script. It catches most of what this lesson covers.
- Be careful with
curl ... | bash: you’re running code you haven’t read. Download it, read it, then run it.
Try it
Up to 15 XPReview this cleanup script
You’re reviewing a colleague’s pull request. Click each line that could cause a disaster.
Click every suspicious section. There are 4.
Key takeaways
There’s no undo, so preview with echo or ls.
Quote variables and guard them with ${var:?}.
Chain with && so a failure stops the next step.
Run ShellCheck, and read scripts before you pipe them to bash.
Lesson quiz
5 questions · pass with 4 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write Bash scripts
Write a script in the editor and run it for real against sample input. Each run gets a fresh Linux sandbox with Bash 5.2 and the GNU tools on Wandbox, a free public service - so experiment freely, even with rm. Your script and test input are sent there.
A dry-run cleaner
Write a function clean that takes a folder. It must stop with an error if the folder is empty or missing, using ${1:?}. It deletes nothing: for each .tmp file in the folder it prints would delete NAME, and it must handle names with spaces. The starter calls it for you.
- Previews safely
Your script runs with Bash 5.2 and GNU tools on Wandbox, a free public service, in a fresh sandbox each time. Your script and test input are sent to that service.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…