The Shell & Terminal¶
Every automation task, every server deployment, every data pipeline — it all runs through the shell at some point. Learning to use the shell is not just a skill for sysadmins; it is the foundation of every developer's power-user toolkit.
Learning Objectives¶
By the end of this section you will be able to:
- Explain the difference between a shell and a terminal emulator
- Navigate the filesystem using
pwd,cd, andls - Read documentation using
manpages - Use tab completion and command history to work faster
- Identify which shell you are running and check its version
What Is a Shell?¶
A shell is a program that accepts text commands, interprets them, and passes them to the operating system kernel for execution. Think of it as a translator sitting between you and the hardware.
The most common shell on Linux and macOS is bash (Bourne Again SHell). Most scripts in this course target bash specifically.
bash vs zsh
macOS changed its default shell to zsh in Catalina (2019). For scripting, bash and zsh are nearly identical at the level of this course. Scripts start with #!/usr/bin/env bash to be explicit about which interpreter to use.
Shells vs Terminal Emulators¶
These two terms are often used interchangeably, but they are different programs:
| Term | What it is | Examples |
|---|---|---|
| Shell | The program that interprets commands | bash, zsh, sh, fish, ksh |
| Terminal emulator | The window/app that displays the shell | GNOME Terminal, iTerm2, Windows Terminal, Alacritty |
The terminal emulator handles the visual side — colors, fonts, scrollback buffer. The shell handles the logic — what happens when you type a command.
Common Shells¶
| Shell | Where you will see it | Notes |
|---|---|---|
sh |
POSIX scripts, minimal containers | Oldest, most portable |
bash |
Linux servers, most scripts | Most common for scripting |
zsh |
macOS default, power users | Better interactive experience |
fish |
Developer workstations | Friendly, but non-POSIX |
dash |
Ubuntu's /bin/sh |
Faster than bash, fewer features |
Check your shell
Navigating the Filesystem¶
These three commands are the ones you will type thousands of times. Learn them now and use them constantly.
pwd — Print Working Directory¶
pwd tells you where you are. Run it whenever you feel lost.
ls — List Directory Contents¶
total 48
drwxr-xr-x 8 user user 4096 Jan 15 09:23 .
drwxr-xr-x 3 root root 4096 Jan 10 08:00 ..
-rw------- 1 user user 220 Jan 10 08:00 .bash_logout
-rw------- 1 user user 3526 Jan 10 08:00 .bashrc
drwxr-xr-x 2 user user 4096 Jan 15 09:23 Desktop
drwxr-xr-x 3 user user 4096 Jan 12 14:05 Documents
drwxr-xr-x 2 user user 4096 Jan 14 11:30 Downloads
drwxr-xr-x 4 user user 4096 Jan 15 09:10 scripts
The -l flag gives long format (permissions, owner, size, date). The -a flag shows hidden files (files whose names start with .). You can combine flags: -la or -l -a.
cd — Change Directory¶
cd .. # go up one level
cd ~ # go to your home directory
cd - # go back to where you just were
cd /etc # absolute path — starts from filesystem root
Tab completion
Type the first few characters of a path and press Tab. Bash completes it automatically. If multiple matches exist, press Tab twice to see all options. Use this constantly — it prevents typos and saves time.
Reading Documentation with man¶
man opens the manual page for any command:
This opens the manual in a pager (usually less). Navigation:
| Key | Action |
|---|---|
j / k |
Scroll down / up one line |
Space |
Next page |
b |
Previous page |
/pattern |
Search for text |
n |
Next search match |
q |
Quit |
man page sections
Man pages are divided into sections. Section 1 is user commands, section 5 is file formats, section 8 is system administration. If a name exists in multiple sections (e.g., passwd), specify the section: man 5 passwd.
When man is not enough:
ls --help # short built-in help for most commands
type ls # tells you whether ls is a builtin, alias, function, or binary
which bash # shows the full path to the bash executable
command -v bash # POSIX-portable version of which
Command History¶
bash saves commands you run in ~/.bash_history. Access your history:
history # list recent commands with line numbers
history 20 # last 20 commands only
!! # re-run the last command
!ls # re-run the last command that started with ls
Ctrl+R # reverse search — type to find a previous command
Build the habit now
Tab completion and Ctrl+R are the two single biggest speed improvements for working in the shell. Use them from your very first session. After a week they become automatic.
Your First Session¶
A typical navigation session from scratch:
total 48
drwxr-xr-x 8 user user 4096 Jan 15 09:23 .
drwxr-xr-x 3 root root 4096 Jan 10 08:00 ..
-rw------- 1 user user 3526 Jan 10 08:00 .bashrc
drwxr-xr-x 2 user user 4096 Jan 15 09:23 Desktop
drwxr-xr-x 3 user user 4096 Jan 12 14:05 Documents
drwxr-xr-x 4 user user 4096 Jan 15 09:10 scripts
Common Mistakes¶
Case sensitivity
Linux filesystems are case-sensitive. Documents, documents, and DOCUMENTS are three different directories. Windows and macOS are case-insensitive by default, so this surprises people coming from those platforms.
Spaces in paths
A space in a filename breaks naive commands. Always quote paths that might contain spaces: cd "My Documents", not cd My Documents. The second version tells bash to cd to My — which probably does not exist.
Confusing shells and terminals
If someone says "open a terminal," they mean open a terminal emulator. If someone asks "which shell are you using?", they mean bash/zsh/fish. The terminal is just a window; the shell is the program interpreting your commands.
Practice Exercises¶
Warm-Up (run and observe)¶
- Open a terminal. Run
pwd. Note your home directory path. - Run
ls -la ~. Identify three hidden files (names starting with.). - Navigate to
/etcusingcd /etc. Runls | head -20. Navigate back withcd ~. - Run
man ls. Find the flag that sorts files by modification time. Pressqto exit. - Run
type cdandtype ls. Why does one say "shell builtin" and the other a file path?
Main (write a short script)¶
Create ~/scripts/practice/explore.sh:
#!/usr/bin/env bash
set -euo pipefail
echo "=== Current directory ==="
pwd
echo ""
echo "=== Contents ==="
ls -la
echo ""
echo "=== Your shell ==="
echo "$SHELL"
echo ""
echo "=== System info ==="
uname -a
Run it:
Stretch¶
- Use
man bashto find what theHISTSIZEandHISTFILESIZEvariables control. What are the defaults on your system? (Hint: check~/.bashrc.) - Press
Ctrl+Rand search for a command you ran earlier in this session. How do you execute it? How do you cancel the search without running anything? - Run
echo $PATH. What is$PATH? Why is your current directory usually not in it?
Interview Questions¶
- What is the difference between a shell and a terminal emulator?
Show answer
A terminal emulator is the GUI application that provides a text window (examples: GNOME Terminal, iTerm2, Windows Terminal). A shell is the program running inside that window that interprets your commands (examples: bash, zsh, fish). When you close the terminal window, both programs exit — but they are distinct programs with distinct responsibilities.
- How do you find out which shell you are currently running?
Show answer
Run echo $SHELL to see your default login shell (what opens when you start a terminal). Run echo $0 to see the name of the currently active shell process. These can differ if you launched a different shell explicitly (e.g., typed zsh inside a bash session).
- What does
cd -do?
Show answer
It changes to the previous working directory — whatever $OLDPWD contained before your last cd command. It is useful for toggling between two directories without typing their full paths. bash prints the directory it switched to.
- Why would you use
man 5 passwdinstead ofman passwd?
Show answer
man passwd (section 1) shows the man page for the passwd command used to change passwords. man 5 passwd shows the man page for the /etc/passwd file format. When a name has pages in multiple sections, you must specify the section number to get the right one.
- What does
type lstell you thatwhich lsdoes not?
Show answer
type ls tells you whether ls is a shell builtin, an alias, a function, or an external binary — and shows the full definition if it is an alias or function. which ls only searches $PATH for external binaries; it cannot reveal aliases or shell builtins.