Working Code
Open a terminal and type your first command. On macOS, press Spotlight (Cmd+Space) and search for "Terminal," or go to Applications > Utilities > Terminal.
Example 1: Printing text
echo "Hello, Terminal!"
Output:
Hello, Terminal!
echo prints text to the screen. It's one of the simplest commands.
Example 2: Checking your current location and listing files
pwd
Example output:
/Users/dale
ls
Example output:
Desktop Documents Downloads Movies Music Pictures
Example 3: Adding options to a command
ls -l
Example output:
drwx------ 5 dale staff 160 Jan 15 10:30 Desktop
drwx------ 10 dale staff 320 Jan 14 09:00 Documents
drwx------ 3 dale staff 96 Jan 13 15:20 Downloads
Characters like -l that start with - are called flags (or options). They modify how a command behaves.
Try printing text with echo. Check your location with pwd and list files with ls. Type help to see available commands.
Try It Yourself
Once you understand command structure, picking up new commands becomes easy.
Command structure: verb + flags + arguments
ls -la Documents
^ ^ ^
verb flags argument (target)
- Verb (command): what to do (
ls,echo,pwd) - Flags (options): how to do it (
-l,-a,-r) - Arguments: what to do it on (
Documents,file.txt)
Try combining different commands:
# Detailed list including hidden files
ls -la
# View contents of a specific directory
ls -l Documents
# Save a message to a file
echo "My first file" > hello.txt
# Read file contents
cat hello.txt
You can combine multiple flags. ls -l -a and ls -la are identical.
Try ls -la to see all files including hidden ones. Create a file with echo text > filename.txt.
"Why?" — Why Use the Terminal?
The terminal is a text-based interface for giving commands to your computer. Tasks that require multiple clicks in a GUI can be done with a single command.
| Scenario | GUI | Terminal |
|---|---|---|
| Rename 1000 files | Click one by one (practically impossible) | One command |
| Connect to a server | No GUI available | SSH |
| Run automation scripts | Not possible | Instant |
| Search/replace text | One at a time in an editor | One line with grep/sed |
Understanding the Prompt
When you open the terminal, you see something like this:
dale@macbook ~ %
dale— current usernamemacbook— computer name~— current location (~means the home directory)%— waiting for input (bash uses$, zsh uses%)
stdin and stdout — The Flow of Data
This is a core terminal concept:
Keyboard -> [stdin] -> Command -> [stdout] -> Screen
- stdin (standard input): the input channel where a command receives data (default: keyboard)
- stdout (standard output): the output channel where a command sends results (default: screen)
When you type echo "hello":
echoreceives"hello"as an argument (not through stdin)- It outputs
helloto stdout (the screen)
This concept matters because it's the foundation for pipes (|) and redirection (>) that you'll learn later.
Common Mistakes
Mistake 1: Case sensitivity
# Wrong
LS
Echo "hello"
# Correct
ls
echo "hello"
Terminal commands are case-sensitive. LS is not the same as ls.
Mistake 2: Watch out for spaces
# This treats "hello" and "world.txt" as two separate files
ls hello world.txt
# This treats "hello world.txt" as one filename
ls "hello world.txt"
If a filename contains spaces, always wrap it in quotes.
Mistake 3: Command not found
zsh: command not found: sl
Either you made a typo or the program isn't installed. Check that you typed ls, not sl.
Deep Dive
What's the difference between bash and zsh?
A shell is the program that interprets and runs commands. The terminal is just the window that runs a shell.
- bash — the classic default shell. Widely used on Linux servers.
- zsh — the default shell on macOS since Catalina (2019). Has more features than bash.
Check which shell you're using:
echo $SHELL
Output:
/bin/zsh
This course uses commands that work in both bash and zsh.
Choosing a terminal app: default Terminal vs iTerm2
Both the built-in macOS Terminal and iTerm2 are solid choices. iTerm2 adds:
- Tabs and split panes
- Better color support
- Search functionality
Start with the default Terminal. Switch to iTerm2 later if needed. The commands are the same.
Command history and tab completion
You don't need to retype commands:
Up/Downarrow keys — previous/next commandCtrl+R— search history (type to find matching commands)Tab— auto-complete filenames/commands
# Type "ls Doc" then press Tab:
ls Documents/
These three shortcuts alone cut your typing in half.
- Open the terminal and type
echo "My first command". - Check your current location with
pwd. - List files with
ls, then check hidden files withls -la. - Create a file with
echo "Hello" > greeting.txt, then read it withcat greeting.txt. - Check the details of the file you just created with
ls -l greeting.txt.
Try echo, pwd, and ls. Use ls -la to see hidden files, and create a file with echo text > file.txt.
Which option correctly matches each part of ls -la Documents?