Learn Bash

The standard Unix shell for running commands and writing scripts

26 lessons · 5 hours · 4 levels · covers Bash 5.2

Bash (the Bourne Again SHell) is the command interpreter on most Linux systems and the language of countless build, deploy and automation scripts. It glues programs together with pipes, redirection and variables, and adds loops, functions and arrays for real scripting. Knowing it well makes you faster at the terminal and lets you read and write the scripts that run CI pipelines, containers and servers.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics9 lessons · 1 hour in total
2. PracticalUse it productively at work, with the usual idioms17 lessons · 3 hours in total
3. AdvancedThe less common features, performance and trade-offs22 lessons · 4 hours in total
4. ExpertInternals and the edge cases experts know26 lessons · 5 hours in total

The Bash tree

BashThe shell and running commandsThe shell and runningcommandsVariablesVariablesQuoting: single, double and noneQuoting: single,double and noneExit status, && and ||Exit status, && and||Writing and running a scriptWriting and running ascriptif and test conditionsif and test conditionsfor and while loopsfor and while loopsRedirection: >, >>, 2> and <Redirection: >, >>,2> and <PipelinesPipelinesParameter expansion tricksParameter expansiontricksCommand substitution and arithmeticCommandsubstitution and ari…FunctionsFunctionsIndexed and associative arraysIndexed andassociative arrayscase statements and glob patternscase statements andglob patternsReading input: read and here-docsReading input: readand here-docsEnvironment variables and startup filesEnvironmentvariables and start…Safer scripts: set -euo pipefailSafer scripts: set-euo pipefail
Essentials and Practical shown (17 of 26 lessons); the full list is below. Click a lesson to read it.

All 26 lessons, in order

Getting started

  1. The shell and running commands · Essentials · 8 min

    What a shell is, how a command line is split into a command and arguments, and how to find help.

  2. Variables · Essentials · 8 min

    Create, read and remove shell variables, and learn the special parameters bash sets for you.

  3. Quoting: single, double and none · Essentials · 10 min

    How single quotes, double quotes and backslashes control which characters bash interprets.

Conditions and loops

  1. Exit status, && and || · Essentials · 7 min

    Every command returns a number: 0 means success. Chain commands on success or failure with && and ||.

  2. if and test conditions · Essentials · 12 min

    Branch with if/elif/else using [[ ]] tests on strings, numbers and files, and (( )) for arithmetic.

  3. for and while loops · Essentials · 10 min

    Repeat work over lists, files, number ranges and conditions with for, while and until.

  4. case statements and glob patterns · Practical · 10 min

    Match strings against glob patterns with case, and master *, ?, [...] and extended globs.

Functions and scripts

  1. Writing and running a script · Essentials · 10 min

    Put commands in a file with a shebang, make it executable and read its arguments.

  2. Functions · Practical · 12 min

    Define reusable functions with local variables, arguments, return statuses and output.

  3. Environment variables and startup files · Practical · 10 min

    export, per-command variables, PATH, and which of .bashrc and .bash_profile runs when.

  4. Parsing options with getopts · Advanced · 10 min

    Accept -v, -o FILE and bundled flags properly with the getopts builtin and a usage message.

  5. Background jobs, wait and parallelism · Advanced · 12 min

    Run commands in the background with &, wait for them, check their status and parallelise with xargs -P.

Input, output and pipelines

  1. Redirection: >, >>, 2> and < · Essentials · 10 min

    Send a command's output and errors to files, read input from files and silence noise with /dev/null.

  2. Pipelines · Essentials · 10 min

    Connect commands with | so each one's output becomes the next one's input.

  3. Reading input: read and here-docs · Practical · 12 min

    Read lines and fields safely with while read, and feed text into commands with here-docs and here-strings.

  4. Subshells, grouping and process substitution · Advanced · 12 min

    Know when code runs in a subshell, group commands with { } and ( ), and treat command output as a file with <( ).

Expansions, strings and arrays

  1. Parameter expansion tricks · Practical · 12 min

    Defaults, substrings, prefix/suffix stripping, search-and-replace and case changes, all without external tools.

  2. Command substitution and arithmetic · Practical · 10 min

    Capture a command's output with $(...) and do integer math with $(( )) and (( )).

  3. Indexed and associative arrays · Practical · 12 min

    Store lists and key/value maps in arrays, loop over them safely and pass them on intact.

Robust scripting

  1. Safer scripts: set -euo pipefail · Practical · 12 min

    Make scripts stop on errors, unset variables and failed pipeline stages, and know the exceptions.

  2. trap: cleanup and signals · Advanced · 10 min

    Run cleanup code on exit, errors and signals so temporary files and locks never leak.

  3. Debugging and ShellCheck · Advanced · 10 min

    Trace scripts with set -x and a better PS4, check syntax with bash -n and catch bugs early with ShellCheck.

Internals and portability

  1. The order of expansions and word splitting · Expert · 15 min

    The exact sequence bash applies to every command line, and how IFS, $@ and $* really behave.

  2. How bash runs commands: fork and exec · Expert · 12 min

    Command lookup order, builtins versus programs, fork/exec, $$ versus $BASHPID, lastpipe and exec.

  3. File descriptors and exec redirections · Expert · 12 min

    Open, duplicate, swap and close file descriptors, redirect a whole script and use coprocesses.

  4. POSIX sh, bashisms and when to stop · Expert · 12 min

    Know which features are bash-only, how /bin/sh differs across systems and when a script should become Python.