Skip to content
mentoor

Topic 1 of 5 · 4 min read

Reading a command before approving it

Goal: Explain what an agent's proposed shell command will do, including globs, redirects and empty variables, before approving it.

Before you approve a shell command, say out loud what it will touch, what it will change, and what it will do if a variable is empty. Agents such as Claude Code, Codex, Cursor and Copilot propose commands all day. Approving them without reading is how a small slip becomes lost work.

Check yourself · 1 of 3

An agent proposes rm -rf "$OUT"/dist. What happens if OUT is unset and the command runs as written?

Choose one answer

Why it matters

The shell does not run your command as typed. It first rewrites it: it expands globs, variables and quotes, and only then runs the result. The command you see is not always the command that runs. A model can also write a plausible command that is wrong in one character.

How to read one

Read left to right, in four passes.

  1. The program and its flags. What does the main command do? Check any flag you do not know in the manual (man rm, or rm --help). Flags like -r (recursive), -f (force, no questions) and --hard change the risk completely.
  2. The targets. Which paths? A glob such as *.log is replaced by every matching file in the current directory, and * alone is everything. Ask: which directory am I in?
  3. The expansions. In rm -rf "$DIR/build", what if DIR is unset or empty? The path becomes /build. Unquoted, rm -rf $DIR/build with a path that contains a space splits into two targets. Quote variables, and use ${DIR:?} so an empty value stops the command instead of continuing.
  4. The plumbing. > overwrites a file, >> appends, | feeds output into another command, && runs the next command only on success, ; runs it regardless. A long chain is several commands: read each one.

Before and after

Weak, as proposed by an agent:

cd $BUILD_DIR && rm -rf *

If BUILD_DIR is empty, cd with no argument goes to your home directory, and rm -rf * deletes it. The fixed version fails safe:

rm -rf -- "${BUILD_DIR:?BUILD_DIR is not set}"/dist

It names the exact folder, quotes the variable and aborts if it is empty. You can also run echo rm -rf ... first: echo prints the command after expansion, so you see the real paths without running anything. Paste scripts into ShellCheck to catch unquoted variables.

What goes wrong

  • Trusting the working directory. The agent assumes it is in the project folder. A previous cd may have moved it. Ask for absolute paths or an explicit cd that is checked.
  • A redirect that overwrites. echo "$X" > .env replaces the whole file. The model meant to add one line.
  • Chains that hide the risky part. The safe-looking ls at the start of a long line does not make the last rm safe.

When you cannot explain a command in one plain sentence, do not approve it. Ask the agent to explain it, or to run a read-only version (ls, echo, --dry-run) first.

Examples

Empty variable in a delete

rm -rf $APP_DIR/cache

If APP_DIR is empty this runs rm -rf /cache, and with an unquoted path that contains a space it splits into two targets. Fix: rm -rf -- "${APP_DIR:?}"/cache. The -- ends option parsing, the quotes keep one argument, and :? aborts when the value is missing.

The overwriting redirect

echo "API_URL=https://staging.example.com" > .env

The agent wanted to add a line. The single > replaces the whole file and every other setting is gone. Use >> to append, or open the file in an editor and review the diff.

Key points

  • The shell expands globs and variables before running, so read the command as it will be after expansion.
  • An empty or unset variable can turn a narrow path into a wide one, such as /build or your home directory.
  • Quote every variable, and use the :? form so an empty value stops the command.
  • Redirection with > overwrites a file; >> appends; pipes and && chains are several commands to read one by one.
  • Prefix a risky command with echo or use a dry-run flag to see the real targets first.

Common mistakes

  • Approving a command because the first word looks harmless, without reading the chain after it.
  • Not checking which directory the command runs in before trusting a relative path or a glob.
  • Leaving variables unquoted so spaces or an empty value change what gets deleted.
  • Using the agent's own explanation as proof instead of checking the flags yourself.

Check yourself · 2 of 3

Which change makes echo "$LINE" > notes.txt safer when you only want to add a line to an existing file?

Choose one answer

Check yourself · 3 of 3

You want to see what the glob in rm -rf build/*.tmp will match before deleting anything. What is the best first step?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic