Lesson 8 of 18Your tools
The Terminal
Move around your computer, manage files, chain commands with pipes, and find out exactly which program runs and why it failed.
You now know what happens inside the machine and across the network. This unit gives you the tools to work with both. Every developer tool you will use, from Git to Python to Docker, is driven from the terminal first and wrapped in buttons second (if at all). Tutorials, docs and error messages all assume you can type a command and read what comes back. It feels strange for about a week. After that it is faster than clicking, and you will wonder how you managed without it.
Terminal, shell, command line
Three words that people use loosely:
- The terminal is the window: the app that shows text and takes your typing. On macOS it is Terminal (or iTerm2, Ghostty, Warp). VS Code has one built in.
- The shell is the program running inside it that reads your commands and runs them. macOS uses zsh by default; most Linux systems use bash. They are nearly identical for everyday work.
- The command line or prompt is the line where you type, usually ending in
$or%.
A command is a program name followed by arguments (what to work on) and options or flags (how to do it), separated by spaces:
ls -l ~/code# program: ls option: -l (long format) argument: ~/codeWhere am I? Paths
Your files live in a tree of folders (the shell calls them directories). At any moment, your shell is "standing" in one of them: the current working directory. Commands work relative to it unless you say otherwise.
- /the root: the top of everything
- Users/on Linux this is /home
- ada/your home folder, also called ~
- code/
- notes-app/
- main.py
- README.md
- portfolio/
- Desktop/
- Documents/
- resume.pdf
- usr/
- bin/system programs live here
- python3
- etc/system settings
A path says where a file is:
- An absolute path starts at the root with
/, like/Users/ada/code/notes-app/main.py. It means the same thing from anywhere. - A relative path starts from where you are. Standing in
/Users/ada/code, the pathnotes-app/main.pypoints to the same file.
A few shortcuts work in any path:
| Symbol | Means | Example |
|---|---|---|
~ | Your home folder | cd ~/code |
. | The current folder | python3 ./main.py |
.. | The folder one level up | cd .. |
/ at the start | The root | cd /etc |
Python can resolve paths the same way the shell does. Run this, then try your own:
Press Run (⌘ Enter) to see what happens.You are in /Users/ada/code/notes-app and run cd ../../Documents. Where are you now?
import posixpath
print(posixpath.normpath("/Users/ada/code/notes-app/../../Documents"))Moving around and looking
These three commands are most of your terminal life:
pwd # print working directory: where am I?ls # list what is herels -la # long format, including hidden files (names starting with .)cd code # change directory into codecd .. # up one levelcd ~ # home (plain "cd" does this too)cd - # back to the previous directoryTab completion saves you a lot of typing and typos. Type cd Doc and press Tab: the shell fills in Documents/. If more than one name matches, press Tab twice to see the options. Use it constantly.
Your shell also remembers history. Press the up arrow to bring back the previous command, and Ctrl+R to search through everything you have typed.
Creating, copying, moving, deleting
mkdir notes-app # make a directorymkdir -p notes-app/src/utils # make nested directories in one gotouch notes-app/main.py # create an empty file (or update its timestamp)cp main.py backup.py # copy a filecp -r src src-old # copy a directory (-r means recursive: everything inside)mv backup.py old/ # move a file into a foldermv draft.txt final.txt # mv also renamesrm final.txt # delete a filerm -r src-old # delete a directory and everything in itTo look inside files:
cat main.py # print the whole fileless server.log # scroll through a long file (q to quit, / to search)head -n 20 data.csv # first 20 linestail -f server.log # follow a log as new lines arrive (Ctrl+C to stop)Starting in your home folder, set up a new project with an empty main.py and check it worked.
- Tap the steps below in the right order.
Finding things, pipes and redirects
grep searches text for a pattern and prints matching lines:
grep "TODO" main.py # lines containing TODOgrep -rn "TODO" src # search every file under src, with line numbersgrep -i "error" server.log # ignore upper/lower caseThe real power of the shell is chaining small programs. A pipe (|) sends the output of one command into the next as its input. A redirect sends output into a file instead of the screen: > overwrites the file, >> appends to it.
cat server.log | grep "ERROR" | wc -l # how many error lines?history | grep "git" # which git commands have I run?ls -l > listing.txt # save the listing to a fileecho "DEBUG=true" >> .env # add a line to the end of .envcat reads the file and writes every line to its output.
Here is the same idea in Python, so you can see what each stage does to the data:
Press Run (⌘ Enter) to see what happens.notes.txt has 10 lines. You run echo "hello" > notes.txt. How many lines does it have now?Running programs, PATH and environment variables
When you type python3 main.py, how does the shell find python3? It checks a list of directories stored in an environment variable called PATH. An environment variable is a named value that every program started from the shell can read.
echo $PATH # the folders searched for commands, separated by :which python3 # which file runs when I type python3?# /usr/bin/python3echo $HOME # your home folderexport API_URL="http://localhost:8000" # set a variable for this shell session"Command not found" almost always means the program is not installed, or its folder is not in PATH. Installers often tell you to add a line like export PATH="$HOME/.local/bin:$PATH" to your shell's startup file (~/.zshrc for zsh, ~/.bashrc for bash), which runs every time a terminal opens.
Running your own code is just another command:
python3 main.py # run a Python filenode index.js # run a JavaScript file./build.sh # run a script in the current folder (the ./ matters)If a program is stuck or running forever, press Ctrl+C to stop it. That is the most important key combination in this lesson. Ctrl+D ends input (and closes a Python prompt), and clear (or Ctrl+L) tidies the screen.
Match each task to a command.
Getting help fast
You will meet commands you have never seen every week. Nobody memorises flags; the skill is finding the answer in under a minute without leaving the terminal.
ls --help # a short usage summary (GNU tools on Linux; many macOS tools reject --help)man ls # the full manual (space to page, /word to search, q to quit)tldr tar # short real-world examples, after installing tldrhelp cd # bash only: help for a shell builtinTwo things trip people up. First, macOS ships BSD versions of many tools while Linux ships GNU versions. They share a name but not every flag: sed -i and date -d behave differently, and ls --help prints an error on macOS but a full summary on Linux. When a Stack Overflow answer does not work on your Mac, this is the usual reason, and man on your own machine is the source of truth. Second, commands like cd and export are not programs at all but builtins, part of the shell itself, so their documentation lives in the shell's manual (help cd in bash, man zshbuiltins in zsh).
Shell Power Tools goes further: how to read a man page synopsis and the keyboard shortcuts worth learning on day one.
Which program actually runs?
You installed Python 3.14, yet python3 --version says 3.9. This is one of the most common setup problems in the industry, and it always has the same cause: there is more than one python3 on your machine, and the shell found a different one first.
When you type a command name, the shell checks, in order:
- Aliases, short names you defined, like
llforls -la. - Functions defined in your shell startup files.
- Builtins, commands built into the shell, like
cd,echoandexport. - The folders in
PATH, left to right. The first match wins and the search stops.
The shell first checks whether python3 is an alias or a function you defined. It is not.
Three commands answer "what will run?", and they are not equal:
which -a python3 # every python3 on PATH, in search ordertype python3 # what the shell will actually do, including aliases and builtinscommand -v python3 # the portable form for scripts: prints the path, or nothingHere is real output from one Mac. Three different Pythons, and the first wins:
/Library/Frameworks/Python.framework/Versions/3.14/bin/python3 <- python.org installer
/usr/local/bin/python3 <- an old Homebrew install
/usr/bin/python3 <- Apple's, for the systemPrefer type when you are debugging, because it knows about aliases and builtins, and which sometimes does not. In bash on macOS, which cd prints /usr/bin/cd, a stub file, even though the cd you type is the builtin; type cd correctly says cd is a shell builtin. In zsh, which is itself a builtin and behaves more like type. In scripts, use command -v: it is defined by POSIX, so it works the same in every shell, and it exits with a non-zero code when nothing is found.
The search is simple enough to simulate. Change PATH in the last lines and watch the answer change:
Press Run (⌘ Enter) to see what happens.PATH is /opt/homebrew/bin:/usr/local/bin:/usr/bin. Only the last two folders contain a python3. Which file runs?
path = ["/opt/homebrew/bin", "/usr/local/bin", "/usr/bin"]
has_python3 = {"/usr/local/bin", "/usr/bin"}
for folder in path:
if folder in has_python3:
print(folder + "/python3")
breakTwo more details explain most of the remaining mysteries:
- The shell remembers locations. After it finds a program once, bash and zsh cache the path. If you then install a newer copy earlier in
PATH, the old one can keep running until you open a new terminal or runhash -r(bash) orrehash(zsh). - Shims. Version managers such as pyenv, mise and asdf can put a folder of tiny shim scripts at the front of
PATH. The shim calledpython3reads the project's version file and hands off to the real interpreter. Sowhich python3points at the shim, not at Python, and the manager's own command (pyenv which python3,mise which python3) tells you where it really goes. uv sidesteps most of this:uv runpicks the project's Python directly.
On Windows, typing python on a fresh machine may open the Microsoft Store. That is an "app execution alias" in a WindowsApps folder on PATH. Turn it off in Settings under App execution aliases once you have installed Python properly.
Exit codes in practice
Operating System Basics showed that every program ends with an exit code, where 0 means success, and that the shell keeps the last one in $?. Using it at the terminal has three gotchas.
$? is replaced by every command, including echo. Check it immediately, or save it:
ls nope 2>/dev/nullecho $? # 1: ls failedecho $? # 0: this is the exit code of the first echoA pipeline reports the exit code of its last command only. pytest | tee log.txt exits 0 even when tests fail, because tee succeeded. In CI that turns a red build green without anyone noticing. Shell Scripting shows the fix, set -o pipefail.
Exact numbers below 128 belong to each program, and even differ by platform: ls on a missing file exits 1 on macOS (BSD ls) but 2 on Linux (GNU ls). grep exits 1 when it simply found nothing, which is not a crash. So scripts should test for "zero or not zero", and only look for a specific number when the tool documents it.
ruff: command not found and exit code 127. The same command works on your laptop. What is the most likely cause?Stop, pause or quit: Ctrl+C, Ctrl+D, Ctrl+Z
Three key combinations look alike and do very different things:
| Keys | Sends | Effect |
|---|---|---|
| Ctrl+C | SIGINT, a polite "interrupt" | Most programs stop. Python raises KeyboardInterrupt. |
| Ctrl+D | End of input | "There is nothing more to read." Closes a Python or Node prompt, and closes the shell itself if the line is empty. |
| Ctrl+Z | SIGTSTP, "suspend" | Pauses the program and gives you the prompt back. It is not stopped; it is frozen. |
Ctrl+Z is the one that bites. Press it instead of Ctrl+C on a dev server and the server is frozen in the background, still holding its port. Start it again and you get Address already in use, even though the old one is not answering anything. The shell's job control commands deal with it:
jobs # list paused or background jobs, e.g. [1] + suspended npm run devfg # bring the most recent job back to the front (then Ctrl+C it properly)bg # let it carry on running in the background insteadkill %1 # stop job number 1 directlyThese work in bash and zsh, on macOS, Linux and WSL. PowerShell has no Ctrl+Z suspend, and in a Windows console Python prompt, Ctrl+Z then Enter is the end-of-input key that Ctrl+D is elsewhere.
If Ctrl+C does nothing, the program may be ignoring it. Find the process from another terminal and kill it, as Operating System Basics describes. A few programs also have their own exit key: q for less, man and a long git log. For the Vim editor, which Git opens if you commit without -m, press Esc, then type :q! and Enter to quit without saving. Git Basics shows how to make Git open VS Code instead.
Match each situation to the right key or command.
Build it yourself: cd
cd has to turn whatever you type into one absolute path. The trick is to treat the path as a stack of folder names: a normal name is pushed on, .. pops one off, and . does nothing. That "stack" idea comes back in the Algorithms & Patterns course.
Be the cd command
Write the logic behind cd. Given the current directory (always an absolute path) and a target, return the absolute path you would end up in.
cwd = "/home/ada", target = "code" → "/home/ada/code"
cwd = "/home/ada/code", target = ".." → "/home/ada"
cwd = "/home/ada", target = "/etc" → "/etc"
cwd = "/home/ada", target = "./a/../b" → "/home/ada/b"Rules:
- A target starting with
/is absolute: ignorecwd. .means "stay here"...means "go up one". Going up from/stays at/.- Repeated slashes (
a//b) and a trailing slash are ignored. - Return a path with no trailing slash, except the root itself, which is
"/". - You do not need to check that folders exist.
- shell
- The program that reads and runs your commands, like zsh or bash. The terminal is the window it runs in.
- working directory
- The folder your shell is currently in. Relative paths start here.
pwdprints it. - absolute vs relative path
- Absolute starts at the root
/. Relative starts from the working directory. - pipe
|sends one command's output into the next command's input.- environment variable
- A named value, like
PATHorHOME, that programs started from the shell can read. - PATH
- The list of folders the shell searches when you type a command name.
- builtin
- A command built into the shell itself, like
cdorexport, rather than a program file on disk. - shim
- A tiny stand-in program a version manager puts first on PATH, which forwards to the right real version.
- exit code
- The number a program hands back when it ends. 0 means success;
$?holds the last one. - job control
- Pausing (Ctrl+Z), resuming (
fg,bg) and listing (jobs) programs started from your shell.