LEARN · PHASE 00 · LESSON 3

Terminal and Shell

After this lesson you can read what the agent is about to do, decide if it is right, and run it yourself when it is wrong.

Type
Learn
Prerequisites
Lesson 1, Dev Environment
Time
~35 minutes

Learning Objectives

  • Read any shell command the agent proposes and name the file it will touch
  • Run the same command yourself before letting the agent run it
  • Predict the difference between > and >> in a command the agent shows you
  • Recover from a wrong agent command by typing the right one yourself
  • Name three shell commands an agent reaches for on every task

The Problem

Two hours into a session the agent says "I'll delete the tests folder." You approve. It was the wrong folder. The work that lived there — including yesterday's draft, never committed — is gone.

You had a chance to catch this. The command was on the screen. You were reading the prose, not the command.

The shell is not a separate skill. The agent runs every action through a shell. When you ignore the shell, you trust the agent to be right about a language you have never read.

The Concept

The agent does not have a private channel. Every action it takes — reading a file, listing a directory, running tests, editing code — arrives as a shell command on your screen before it runs. You see it. You can copy it. You can run it yourself first.

One concept, three corollaries.

flowchart LR
    A[You describe a goal] --> B[Agent writes a shell command]
    B --> C{You read it}
    C -->|looks right| D[You approve, it runs]
    C -->|looks wrong| E[You type the right one]
    D --> F[Output comes back]
    E --> F
    F --> B
Mermaid source — renders as a diagram where mermaid.js is available

The middle box — you read it — is the only skill this lesson teaches. Everything else is supporting muscle.

Corollary 1 — the commands are ordinary

The verbs an agent reaches for are the ones you already know how to read.

What the agent is doingThe shell verb it uses
Reading a filecat, head, or a tool that wraps one
Listing a directoryls
Searching codegrep -rn
Editing a filesed -i, here-docs, or a tool that writes it
Running testsnpm test, pytest, go test
Inspecting git stategit status, git diff
Deleting somethingrm, rm -rf

None of these are hidden. None are special. They are the same commands you would type.

Corollary 2 — overwriting is a one-character mistake

The single character > overwrites. >> appends. The agent almost never means overwrite when it types >. Read for it.

echo "# Contributing" > CONTRIBUTING.md     # replaces if it exists
echo "## Setup" >> CONTRIBUTING.md          # adds to the end

Corollary 3 — the agent's working directory is yours

A cd inside the agent's tool call affects only that call. The shell prompt you see is the one that matters; if the agent's command has no cd, it runs in the same folder you are in. Check with pwd if you are unsure.

The reframe: the agent's commands are your commands, written down before they run — so learning the shell is learning to read what the agent is about to do.

Build It

Do these in the lab repo from Lesson 1.

1. Read what the agent would have read

cd ~/claude-course-lab
cat src/index.ts

The agent uses this — or its Read tool, which wraps this — every time it opens a file. You have now read the same file the same way.

Pass: you see the contents of src/index.ts on your screen.

2. Run a search the agent would run

grep -rn "sum" src/

This is how an agent checks whether a name is already taken. Read it as: search every file under src/, print the file name and line number for every line containing sum.

Pass: you see file paths and line numbers, not raw blobs.

3. Append to a file from the terminal

echo 'export const double = (x) => x * 2' >> src/index.ts
cat src/index.ts

Notice the >>, not >. If you had typed > the old content is gone. This is the difference between a one-character typo and data loss.

Pass: the original sum line is still there and a new double line was added.

4. Catch a wrong rm before it runs

In a throwaway folder, type — but do not press enter yet:

mkdir -p /tmp/sandbox && cd /tmp/sandbox && echo hi > keep.txt && echo hi > keep2.txt
rm -rf keep

Read it left to right: make a folder, enter it, write keep.txt, write keep2.txt, then rm -rf keep. Predict what remains. Press enter. Confirm your prediction was right.

This is the moment an agent would have approved and moved on.

Pass: you can predict the result before pressing enter, and explain why.

5. Predict, then run

For every command in steps 1–4, before pressing enter say out loud: which files will change, which will not, and whether output will appear. Then run it. If reality disagreed with your prediction, that is the lesson. Write the gap in NOTES.md inside the lab repo.

Pass: you wrote at least one prediction, ran it, and noted the gap.

Use It

CommandWhen you reach for it
cat fileReading what the agent is about to read
grep -rn "x" dir/Checking whether a symbol or string already exists
echo "..." >> fileAppending to a file (notice >>)
echo "..." > fileReplacing a file's contents (notice >)
rm fileDeleting one file
rm -rf dirDeleting a directory; -r means recursive, -f means no confirmation
pwdConfirming the agent's working directory is yours
!!Repeating the last command (often after sudo !!)
Ctrl-rSearching command history by typing a few letters
man cmd or cmd --helpLooking up what one flag does

Exercises

  1. In your lab repo, list the files, read src/index.ts, then list the files again. The list is the same — the read did not create anything. Pass: you can name three commands the agent runs that are read-only and create no new files.
  1. Type echo "first" > notes.txt, then echo "second" >> notes.txt, then echo "third" > notes.txt. Read notes.txt after every step. Pass: you can state the difference between > and >> in one sentence, with the final contents of the file as evidence.
  1. Pretend an agent proposed rm -rf src/. Predict what happens, then run a preview version that only lists what would be removed — find src -type f or ls -R src/ does the job without deleting. Pass: you can name one way to preview a destructive command before running it.
  1. Open a new terminal tab, run pwd, then run git status. Both outputs should be from your lab folder. If not, the agent would have worked in the wrong place. Pass: both commands report the lab folder.

Key Terms

TermWhat people sayWhat it actually means
shell"the terminal"The program that interprets what you type. The terminal is the window; the shell is what runs inside it.
command"a line of code"One verb the shell runs, with arguments after it. grep -rn "x" src/ is one command, not five.
redirect >"save output"Overwrite the file on the right with the left side's output. If the file existed, it is gone.
redirect >>"append output"Add to the end of the file. The old contents survive.
flag"option", "switch"A single-letter or single-word argument after the verb that changes its behaviour. -r in rm -rf means recursive; -f means no confirmation.
working directory"the project folder"One specific folder a command runs in. The agent inherits yours; if it cds, it does so only inside its own tool call.

What this lesson skipped

No pipes beyond cat-into-grep. No subshells, no job control, no tmux, no shell scripting, no quoting rules beyond > vs >>. If a later lesson needs one of those — particularly subshells, which decide how cd survives across an agent's tool call — it will ground it inline. This lesson stays at the level that lets you read the agent's next command before approving it.

Check yourself

3 questions. Answer before you reveal — recognising the right option is not the same as being able to produce it.

  1. A build prints its real errors to stderr. npm test | grep FAIL finds nothing, but the errors are clearly visible when you scroll. Why?

    1. grep only works on stdout
    2. npm writes errors to stderr, and a pipe carries only stdout — you need 2>&1
    3. The pipe was too fast and grep missed the lines
    4. grep is case-sensitive by default
    Show answer

    B. npm writes errors to stderr, and a pipe carries only stdout — you need 2>&1

    Two separate streams leave every program. | wires stdout only, so stderr went straight to your screen and never reached grep. npm test 2>&1 | grep FAIL folds stderr into stdout first. Option D is a real gotcha (grep -i fixes it) but it isn't why you saw nothing.

  2. You need to run a 10-minute migration, and you must close your terminal for a meeting and reconnect afterwards without killing it. Which is right?

    1. migration.sh > log.txt & — the ampersand detaches it from the shell
    2. migration.sh > log.txt — redirecting output already detaches it
    3. tmux new -s migrate, run it inside, then detach with Ctrl-b d and reattach later
    4. Leave the terminal open and lock the screen
    Show answer

    C. tmux new -s migrate, run it inside, then detach with Ctrl-b d and reattach later

    A backgrounded job is still a child of your shell; closing the shell sends it a hangup. Only tmux (or nohup/disown combined with the right conditions) survives a closed window. The ampersand and > solve your attention, not the terminal's lifetime.

  3. Why does echo hi | cd src leave you in the same directory, but (cd src && pwd) prints the src path?

    1. cd is not a real command and only works inside parentheses
    2. The left side of a pipe runs in a subshell whose cd is discarded on exit; parentheses create one you can use deliberately
    3. The pipe runs too fast for cd to take effect
    4. Pipes always reset the working directory before running
    Show answer

    B. The left side of a pipe runs in a subshell whose cd is discarded on exit; parentheses create one you can use deliberately

    cd changes the shell's own state, and a piped command runs in a child subshell — its directory change dies with it. Parentheses are how you ask for exactly that behaviour when you want it: the cd applies inside, and vanishes after. Option C is wrong; it is not timing.