Bash & Shell

Bash and the Unix tools with step-through labs (expansion, redirections, pipelines, set -e, subshells, awk), production workflows, 11 runnable problems and the GNU vs macOS flags.

Overview

This is a working reference for Bash and the standard Unix tools, built around what the shell actually does with a line you type. Each mechanism has a step-through lab you can edit and re-trace, the tables stay for quick lookup, and the page ends with production workflows and eleven problems you run here. It assumes Bash 5 on Linux with the GNU tools; where macOS or BSD differs, the tables say so.

> [!IMPORTANT] macOS ships zsh as the default shell (since 10.15) and /bin/bash 3.2, which has no associative arrays, mapfile or ${var^^}. Install a current Bash and start scripts with #!/usr/bin/env bash.

> [!NOTE] Runnable blocks (bash-run) run on bash 5.2 with GNU coreutils, sed, grep and findutils, and mawk as awk, in an empty temporary directory with no network. Every one makes its own input and prints ok: or FAIL.

What happens when you press Enter

  • Read — readline edits the line; Enter submits it — a here-doc or open quote makes bash keep reading (> prompt)
  • Parse — splits into words and operators: pipelines, lists, redirections — x = 1 is a command named x
  • Expand — brace, tilde, $var, $(( )), $( ), splitting, globbing, quote removal — unquoted $var splits and globs
  • Redirect — in the child, applies , 2>&1 left to right — order of 2>&1 matters
  • Execute — builtins and functions run in this shell; programs are found on PATH, forked, exec'd — cd in a pipeline or ( ) does not change your shell
  • Wait — collects every exit status into $? and PIPESTATUS — only the last stage counts unless pipefail

Sign up free to read the full topic

More Quick References topics

Start free on Cariara · Coding problems · System design studio