Command Line Basics (Linux and macOS)
What Is the Command Line?
In this lesson you will
- Explain what the terminal and shell are
- Open a terminal on your computer
- Run pwd, ls and echo
Introduction
Most of the time you use a computer by clicking. The command line lets you control it by typing short instructions. It looks old-fashioned, but developers, system administrators and data scientists use it every day because it is fast, repeatable and powerful.
The simple explanation
The command line is a way to talk to your computer using text. You type a command, press Enter, and it does the job.
Two words you will hear:
- The terminal is the window where you type.
- The shell is the program inside it that understands your commands. A common one is bash. On many Macs it is zsh, which works almost the same for what you learn here.
Open a terminal
- macOS: open the Terminal app.
- Linux: press Ctrl+Alt+T, or search for Terminal.
- Windows: install WSL (Windows Subsystem for Linux) to get a Linux terminal, or use Git Bash.
You will see a prompt, a short line that ends in $ or %. It means the shell is waiting for you.
Your first commands
In the examples, only type what comes after the $. The lines without a $ are what the computer prints.
Terminal
$ pwd
/home/sampwd means print working directory: it shows which folder you are in. Your folder names will differ.
Terminal
$ ls
Documents Downloads Music Picturesls lists what is in the current folder.
Terminal
$ echo "Hello from the terminal"
Hello from the terminalecho prints text back to you.
Quick quiz
Which command shows the folder you are currently in?
How commands are shaped
Most commands follow this pattern:
command options argumentsFor example ls -l Documents runs ls with the option -l (long listing) on the argument Documents.
Be safe
The terminal does what you say without asking "are you sure?". Be careful with commands that delete things, and never paste a command you do not understand.
Try it yourself
Open a terminal. Run pwd, then ls, then echo "my first command". Which folder were you in?
Summary
- The terminal is the window and the shell is the program that runs your commands.
pwdshows your location,lslists files andechoprints text.- Commands have the shape: command, options, arguments.
Lesson complete
+20 XP
Nice work. Keep going while it is fresh.
Sources and further reading