Shell Basics¶
Understanding what bash is and how it fits into the larger picture of terminals, shells, and command-line interfaces.
What is a Shell?¶
A shell is a program that interprets commands and communicates with the operating system. It's the layer between you and the kernel:
┌─────────────────────────────────────┐
│ User (you) │
├─────────────────────────────────────┤
│ Terminal Emulator (UI) │
├─────────────────────────────────────┤
│ Shell (bash, zsh, fish) │
├─────────────────────────────────────┤
│ Operating System Kernel │
├─────────────────────────────────────┤
│ Hardware │
└─────────────────────────────────────┘
The shell:
- Reads commands you type
- Interprets and expands them (variables, globs, etc.)
- Executes programs
- Returns output to you
Terminal vs Shell¶
These terms are often confused:
Terminal (or terminal emulator)
- The application window you type in
- Examples: Terminal.app, iTerm2, Alacritty, Windows Terminal
- Handles display, fonts, colors, keyboard input
Shell
- The program running inside the terminal
- Interprets your commands
- Examples: bash, zsh, fish, sh
You can run different shells in the same terminal, and the same shell in different terminals.
What is Bash?¶
Bash (Bourne Again SHell) is:
- The default shell on most Linux distributions
- Available on macOS (though zsh is now default)
- A superset of the original Bourne shell (
sh) - Both an interactive shell and a scripting language
Check your current shell:
Or see what shell you're currently in:
Bash Versions¶
Version matters for scripting:
| Version | Notable Features |
|---|---|
| 3.2 | macOS default, last GPLv2 version |
| 4.0 | Associative arrays, mapfile, &>> |
| 4.2 | declare -g, negative array indices |
| 4.3 | declare -n (namerefs) |
| 4.4 | ${var@operator} transformations |
| 5.0 | EPOCHSECONDS, improved arrays |
macOS ships with Bash 3.2 due to licensing. Install Bash 5.x with Homebrew:
Interactive vs Non-Interactive¶
Interactive shell: You type commands, shell responds
Non-interactive shell: Runs a script without user input
This distinction matters for configuration files (covered in Dotfiles).
Login vs Non-Login Shells¶
Login shell: First shell when you log in
- Opens when you SSH into a server
- Reads
.bash_profileor.profile
Non-login shell: Shells started after login
- Opening a new terminal tab
- Running
bashfrom another shell - Reads
.bashrc
Check if current shell is a login shell:
Basic Command Structure¶
Commands follow this pattern:
Examples:
ls # command only
ls -l # command + option
ls -l /home # command + option + argument
ls -la /home /tmp # command + options + multiple arguments
Options¶
Short options start with single dash:
Long options start with double dash:
Arguments¶
Arguments are the targets of the command:
Getting Help¶
man Pages¶
The manual (man) provides detailed documentation:
Navigate with:
Spaceorf- page forwardb- page backward/pattern- search forwardn- next search resultq- quit
--help Flag¶
Most commands support --help:
help Built-in¶
For shell built-ins, use help:
type Command¶
Find out what a command is:
Command Execution¶
When you type a command, bash:
- Parses the input (splits into words)
- Expands variables, globs, etc.
- Searches for the command:
- Aliases
- Functions
- Built-ins
- External programs (via PATH)
- Executes the command
- Returns output and exit status
Exit Status¶
Every command returns an exit status:
0= success1-255= failure (specific meaning varies)
Check the last command's exit status:
Running Multiple Commands¶
Sequential (;)¶
Run commands one after another, regardless of success:
Conditional AND (&&)¶
Run next command only if previous succeeded:
Conditional OR (||)¶
Run next command only if previous failed:
Combining¶
Comments¶
Comments start with #:
Try It¶
-
Check your current shell and version:
-
Explore the
typecommand: -
Practice getting help:
-
Test exit codes:
Summary¶
- A shell interprets commands; a terminal displays them
- Bash is the most common shell on Linux
- Commands follow
command [options] [arguments] - Exit status 0 means success; non-zero means failure
- Use
man,--help, andhelpfor documentation