Running Your Programs in a Shell
For non-trivial programs, it is much more convenient to run them directly from an interactive shell (command line) than via the VS Code console, especially if input is to be taken from a file, or output collected into a file.
Using an interactive shell (i.e., cmd in Windows or Terminal on macOS) is required in this subject, so spend a few minutes getting comfortable with the basics.
Most mistakes in programming are easiest to diagnose by running the program from a shell and reading the output carefully.
Open a shell
Windows (cmd)
- Open Start, search for Run.
- Type
cmdand press Enter. - Optional: pin cmd to Start or make a desktop shortcut.
macOS (Terminal)
- Open Applications then Utilities.
- Double-click Terminal.
- Optional: drag Terminal to your Dock.
Everyday commands (cheat sheet)
| Operation | Windows (cmd) | macOS (Terminal) |
|---|---|---|
| See where you are | The current directory is part of the prompt string | pwd |
| List files in a directory | dir |
ls |
| Change to a subdirectory |
cd directory (Tab completes) |
cd directory (Tab completes) |
| Move up one directory | cd .. |
cd .. |
| Delete a file | del filename |
rm filename |
| Delete a directory | rmdir directoryname |
rmdir directoryname |
| Execute a program |
project or project.exe
|
./project |
| View a text file | type filename |
more filename |
Redirecting input and output
You will often test programs by taking input from a file or saving output to a file.
program < input.txt
program > output.txt
program < input.txt > output.txt
Pro tips
- Use Tab to auto-complete filenames and folder names.
- Use the up arrow to recall recent commands.
- If you create a cmd shortcut on Windows, you can set a default start directory (right-click the shortcut, Properties).
- On macOS, you can drag a folder into Terminal to paste its path.