Loading
0x180Lesson 25 of 28

Don’t delete production

Learn the habits that stop one bad command from ruining your week.

22 min 5-question quiz 1 code exercise
By the end of this lesson you can
  • 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 -u makes 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 XP

Near 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.

/home/crew/work
  • cache
    • a.tmp
    • b.tmp
  • my
  • my notes.txt
  • precious
    • mission-data.db
Up arrow: history. Tab: complete. Ctrl-L: clear. Ctrl-C: cancel.
  1. See the danger
  2. Meet the typo
  3. Add the guard
  4. Quote the name

Habits that save you

  • Preview: put echo in front, or run ls with the same pattern first.
  • Quote every variable: "$file".
  • Use set -euo pipefail in 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 the cd fails, but cd /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 XP

Review this cleanup script

You’re reviewing a colleague’s pull request. Click each line that could cause a disaster.

cleanup.sh (round 1 of 1)Flags found 0/0

Click every suspicious section. There are 4.

#!/bin/bash cd $DEPLOY_DIR rm -rf $OLD_RELEASE/* for f in $(ls *.log); do gzip "$f" done curl -s https://get.example.test/install.sh | sudo bash

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.

Exercise 1

A dry-run cleaner

+25 XP

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
script.sh
Loading editor…

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…