Learn Bash by writing it. Work through the lessons in order and you'll go from your first script to robust, production-grade automation — variables, arguments, I/O, conditionals, loops, functions, error handling, and debugging. Every lesson is code you run.
Before you start
You need a Bash shell to run the scripts in. Pick whichever fits your machine:
Windows
Install WSL (Windows Subsystem for Linux) for a real Bash shell. Run this in PowerShell as Administrator, then restart and launch Ubuntu from the Start menu.
wsl --installmacOS
macOS ships with Bash 3 but defaults to zsh. Install a current Bash with Homebrew so array and string features behave as shown:
brew install bash
bash --versionAlready on Linux
You are ready — open a terminal. Confirm you have Bash 4 or newer so associative arrays and case conversion work:
bash --versionYour First Script
Write and run a real Bash script: the shebang line, three ways to run a script, making it executable, comments, and printing output with echo.
Variables
Create, read, and modify Bash variables. Learn the quoting rules that bite everyone, the difference between shell and environment variables, scope with export and local, and the habits that keep scripts safe.
Arguments & Special Variables
Read the arguments passed to your script with $1, $2, $@, and $#. Use special variables like $0, $?, and $$, and shift through a list of positional parameters.
Input & Output
Read input from the user with read, format output precisely with printf, and feed multi-line text into commands using here-documents and here-strings.
Redirection & Pipelines
Control where a command reads from and writes to: redirect stdout and stderr, append to files, discard output, chain commands with pipes, and capture or stream output with command and process substitution.
Strings & Arrays
Manipulate strings without leaving Bash — length, slicing, search-and-replace, and case conversion — and store lists of values in indexed and associative arrays.
Numbers & Arithmetic
Do math in Bash: integer arithmetic with $(( )), increment counters, compare numbers, and reach for bc or awk when you need decimals.
Conditionals & Test
Make decisions in Bash: if/elif/else, the [[ ]] test command, numeric and string comparisons, file tests, combining conditions, and matching many values with case.
Loops
Repeat work in Bash: for loops over lists and files, while and until loops, C-style counting loops, and controlling them with break and continue.
Functions
Package reusable logic into Bash functions: define them, pass arguments, return status with exit codes, capture output, keep variables local, and call a function from itself.
Exit Codes & Error Handling
Write scripts that fail safely: read exit codes, stop on errors with set -euo pipefail, clean up with trap, and log failures so you can diagnose what went wrong.
Pattern Matching & Regex
Match text in Bash: filename globbing, the =~ regex operator inside [[ ]], and basic vs extended regular expressions with grep — knowing which is which.
Debugging & Quality
Find and prevent bugs in your scripts: trace execution with set -x, check syntax without running, lint with ShellCheck, and adopt the habits that keep scripts robust.