Skip to content

Bash Tutorial

A comprehensive guide to bash shell usage, from fundamentals to advanced scripting and modern tooling.

What You'll Learn

This tutorial covers everything from basic command-line navigation to writing production-ready scripts:

  • Fundamentals - Shell basics, file operations, permissions, and I/O redirection
  • Configuration - Dotfiles, environment variables, aliases, and prompt customization
  • Scripting - Variables, conditionals, loops, arrays, and error handling
  • Tools - Text processing, file finding, modern replacements, and JSON handling
  • Advanced - Job control, subshells, signals, and performance optimization
  • Reference - Quick reference cards and cheat sheets

Learning Path

Beginner

If you're new to the command line, start here:

  1. Shell Basics - Understanding shells and terminals
  2. Navigation - Moving around the filesystem
  3. Files & Directories - Basic file operations
  4. Viewing Files - Reading file contents
  5. Permissions - Understanding Unix permissions
  6. Redirection & Pipes - Connecting commands

Intermediate

Once comfortable with basics:

  1. Dotfiles - Configuring your shell
  2. Environment Variables - PATH and exports
  3. Aliases - Creating shortcuts
  4. Shell Functions - Reusable commands
  5. Scripting Basics - Writing your first scripts
  6. Conditionals - Decision making
  7. Loops - Iteration patterns

Advanced

For deeper understanding:

  1. Arrays - Working with collections
  2. Error Handling - Robust scripts
  3. Job Control - Background processes
  4. Signals - Process communication
  5. Performance - Optimization techniques

Prerequisites

  • A terminal emulator (Terminal.app on macOS, or any Linux terminal)
  • Bash 3.2+ (macOS default) or 5.0+ (Linux/Homebrew)
  • Basic computer literacy

Check your bash version:

bash --version

macOS Note

macOS ships with Bash 3.2 due to licensing (GPLv2 vs GPLv3). For modern features like associative arrays and mapfile, install Bash 5.x via Homebrew:

brew install bash

This tutorial notes when features require Bash 4.0+.

Modern Tools

Throughout this tutorial, we highlight modern replacements for classic Unix tools:

Classic Modern Benefits
ls eza Colors, git integration, tree view
cat bat Syntax highlighting, line numbers
grep ripgrep Faster, respects .gitignore
find fd Simpler syntax, faster
top btop Better visualization

See Modern Replacements for installation and usage.

Conventions Used

Code Blocks

Commands to type:

echo "Hello, World!"

Expected output:

Hello, World!

Prompts

The $ symbol represents your shell prompt - don't type it:

$ whoami    # Type only: whoami
user

Placeholders

Angle brackets indicate values you should replace:

cp <source> <destination>

Notes and Warnings

Pro Tip

Helpful suggestions for better workflows.

Caution

Important warnings about potential issues.

Note

Additional context or explanations.

Quick Start

If you're impatient, here's the minimum to get productive:

# Navigate
cd ~/projects        # Change directory
pwd                  # Print working directory
ls -la               # List files with details

# Files
cat file.txt         # View file
cp src dest          # Copy
mv old new           # Move/rename
rm file              # Remove (careful!)

# Search
grep "pattern" file  # Find in file
find . -name "*.md"  # Find files

# Help
man command          # Manual page
command --help       # Quick help

Now dive into Shell Basics for the full journey.