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?
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.
- The program and its flags. What does the main command do? Check any flag you do not know in the manual (
man rm, orrm --help). Flags like-r(recursive),-f(force, no questions) and--hardchange the risk completely. - The targets. Which paths? A glob such as
*.logis replaced by every matching file in the current directory, and*alone is everything. Ask: which directory am I in? - The expansions. In
rm -rf "$DIR/build", what ifDIRis unset or empty? The path becomes/build. Unquoted,rm -rf $DIR/buildwith 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. - 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
cdmay have moved it. Ask for absolute paths or an explicitcdthat is checked. - A redirect that overwrites.
echo "$X" > .envreplaces the whole file. The model meant to add one line. - Chains that hide the risky part. The safe-looking
lsat the start of a long line does not make the lastrmsafe.
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?
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?
Practice items, not counted towards your mastery.
Start here
- The Missing Semester of Your CS Education (MIT) (opens in a new tab)CourseFree
Free lectures on the shell, shell scripting, data wrangling and command-line tools.
- ShellCheck (opens in a new tab)ToolFree
Paste a script or command and see unquoted variables and other classic traps flagged.
- The Linux Command Line (free book) (opens in a new tab)BookFree
A full book on navigation, permissions, processes, pipes and scripting.
More for this topic
- bash(1) Linux manual page (opens in a new tab)DocsFree
The authoritative source on quoting, expansion, redirection and job control.