Sub topic 3
Terminal basics you need
This chapter covers the terminal skills the rest of the tutorial uses: finding out where you are, moving between folders, creating files, running programs, and reading what comes back. It assumes you have installed software on your computer before and can type commands exactly as written. It does not assume prior terminal experience. If you already use a terminal daily, skim the tables and go to the exercises.
Terminal, shell, and prompt
Three terms are often used as if they meant the same thing.
- A terminal is the window where you type text and read text. It is an application, such as Windows Terminal, Terminal on macOS, or GNOME Terminal on Linux.
- A shell is the program running inside the terminal. It reads each line you type, interprets it, and starts the programs you ask for. Common shells are Bash and Zsh (Linux and macOS), PowerShell, and Command Prompt (
cmd.exe) on Windows. - A prompt is the text the shell prints to show it is ready for input, often ending in
$,%, or>.
The shell matters more than the terminal window, because the shell decides which commands exist and how they behave. Later chapters give commands for both Unix-style shells and Windows shells, so check which one you have.
| Operating system | Typical way to open a terminal | Default shell |
|---|---|---|
| Windows 10/11 | Start menu, search "Terminal" or "PowerShell" | PowerShell |
| macOS | Spotlight (Cmd+Space), search "Terminal" | Zsh |
| Linux | Application menu, or Ctrl+Alt+T on many desktops | Usually Bash |
What happens when you press Enter
When you type a command and press Enter, the shell splits the line into words. The first word is the command. The remaining words are arguments. The shell then looks for something to run: first a built-in command, then a program found in a list of folders (the PATH, covered in a later chapter). The program runs, prints output, and ends with a number called an exit code.
Knowing this sequence explains most early errors. "Command not found" means the shell could not locate a program with that name. It does not mean the program is broken.
The working directory and paths
Every shell session has a working directory, which is the folder that relative paths are measured from. Commands such as python3 hello.py look for hello.py in the working directory unless you give a different path.
A path can be written two ways.
- An absolute path starts from the root of the file system. Examples are
/home/ada/projectson Linux,/Users/ada/projectson macOS, andC:\Users\ada\projectson Windows. - A relative path starts from the working directory. Examples are
projects,./projects, and..\notes.
Two special names work in every shell:
.means the current folder...means the parent folder, one level up.
Windows uses backslashes (\) between folder names, while Linux and macOS use forward slashes (/). PowerShell accepts both. Command Prompt generally needs backslashes in paths.
The commands you will use
These are the commands this tutorial relies on. PowerShell defines aliases so that many Unix names also work there, but the real PowerShell names are shown for clarity.
| Task | Bash / Zsh | PowerShell | Command Prompt |
|---|---|---|---|
| Show working directory | `pwd` | `Get-Location` (alias `pwd`) | `cd` |
| List files | `ls -la` | `Get-ChildItem` (alias `ls`) | `dir` |
| Change directory | `cd folder` | `cd folder` | `cd folder` |
| Go up one level | `cd ..` | `cd ..` | `cd ..` |
| Make a folder | `mkdir name` | `mkdir name` | `mkdir name` |
| Create an empty file | `touch file.py` | `New-Item file.py` | `type nul > file.py` |
| Show a file's contents | `cat file.py` | `Get-Content file.py` | `type file.py` |
| Clear the screen | `clear` | `Clear-Host` (alias `cls`, `clear`) | `cls` |
A plain cd with no argument behaves differently by shell. In Bash and Zsh it goes to your home folder. In Command Prompt it prints the current directory. In PowerShell, cd with no argument does nothing on Windows in older versions and goes home in newer ones, so always give a destination.
Worked example: a folder for your first project
The next chapters need a place to keep scripts. This sequence creates one and confirms you are inside it. First, on macOS or Linux:
cd ~
mkdir python-start
cd python-start
pwd
touch hello.py
ls -laThe same steps in PowerShell:
cd ~
mkdir python-start
cd python-start
Get-Location
New-Item hello.py
Get-ChildItemHere ~ stands for your home folder. After cd python-start, the prompt in most shells shows the new folder name, and pwd or Get-Location prints the full path. The empty hello.py file appears in the listing. You will fill it in when you write your first script.
If mkdir reports that the folder already exists, that is harmless. Run cd python-start and continue.
Spaces, quotes, and special characters
Because the shell splits a line on spaces, a folder name containing a space becomes two arguments unless you protect it.
cd "My Projects"Double quotes work in every shell mentioned here. Single quotes also work in Bash, Zsh, and PowerShell, but not reliably in Command Prompt. In Bash, a backslash before a space (My\ Projects) is another option.
The simplest way to avoid this class of problem is to choose folder and file names with letters, digits, hyphens, and underscores only. Prefer python-start over Python Start. Many tools, including some Python packaging tools, handle spaces poorly even when the shell does not.
Case sensitivity also differs. Linux file names are case sensitive, so Hello.py and hello.py are different files. Windows and default macOS volumes are case insensitive but preserve the case you typed. A script that works on your laptop can fail on a Linux server because of a capital letter.
Editing speed: history and completion
Two features save many keystrokes and many typos.
- Tab completion: type the first letters of a file or folder name and press Tab. The shell completes the name or, after a second press, lists the options. It only completes names that exist, so a failed completion is a useful signal that you are in the wrong folder.
- Command history: press the Up arrow to bring back earlier lines, then edit them. Pressing Up and Enter is the quickest way to rerun a script during development.
Ctrl+C interrupts a program that is running. You will use it to stop a script stuck in a loop. It does not close the terminal.
Exit codes and reading results
Every command finishes with an exit code. By convention, 0 means success and any other number means some kind of failure. You can read the code of the last command:
ls missing-file.txt
echo $?On most systems the first line prints an error and the second prints a nonzero number, commonly 1 or 2. In PowerShell the value is $LASTEXITCODE for external programs, and in Command Prompt it is %ERRORLEVEL%.
You will rarely check exit codes by hand at this stage. They matter because scripts, editors, and automated tools use them to decide whether a step worked. Python exits with 0 when a script finishes normally and 1 when an unhandled error ends it.
Common mistakes
- Running a command in the wrong folder. Symptom: a file you can see in your editor is reported as missing. Fix: run
pwd(orcdin Command Prompt) and compare it with where the file is. - Pasting a command together with its prompt symbol. Examples in books often show
$ ls, where$is the prompt. Type onlyls. - Pasting multi-line text without reading it. Always read what you paste before pressing Enter, especially lines that delete or overwrite files.
- Using a Unix command in Command Prompt, such as
lsortouch, which do not exist there. Use the table above or switch to PowerShell. - Assuming a silent command failed. Many commands, such as
cdandmkdir, print nothing on success.
Check your understanding
Exercise
You run cd python-start and then python3 hello.py, but the shell says it cannot open the file hello.py. Name two commands that help you find the cause, and say what each one tells you.
Show answer
Run pwd (or Get-Location in PowerShell, cd with no arguments in Command Prompt) to see the working directory, and run ls (or Get-ChildItem, or dir) to list the files in that folder. If hello.py is not in the listing, you are in the wrong folder or the file has a different name or extension, for example hello.py.txt.
Exercise
A folder is named My Scripts. Write the command that changes into it from a Bash, Zsh, or PowerShell prompt, and explain why the quotes are needed.
Show answer
cd "My Scripts". The shell splits a line into words at spaces, so without the quotes it would pass My and Scripts as two separate arguments instead of one folder name.
Exercise
After a command finishes, echo $? prints 0. What does that tell you, and what would a nonzero value mean?
Show answer
The value is the exit code of the previous command. 0 means the command succeeded. A nonzero value means it failed, and the specific number is defined by the program that ran.