Bash & the POSIX Shell
Bash and the POSIX shell from the first command to production scripts: expansion, quoting, tests, loops, pipes, functions, text tools and portability.
75 lessons: 75 free with Pro answers.
Examples run with Bash 5.3.20, dash 0.5.13.5 and shellcheck 0.11.0.
Who it is for
Developers, DevOps learners and students who need to automate the command line.
What you will be able to do
- Explain how Bash reads, expands and runs a command line, and quote every word correctly
- Write scripts with variables, tests, loops, functions and arrays that pass ShellCheck
- Redirect and pipe data between programs and process text with grep, sed, awk, find and jq
- Make scripts robust with exit statuses, error handling, traps and debugging
- Build command-line tools with options, help text and meaningful exit codes
- Manage processes, jobs and signals, and set up a fast interactive shell
- Write portable POSIX sh, spot GNU and BSD differences, and keep untrusted input safe
- Test, format and speed up scripts, and choose when another tool fits better
Revise and practise
Modules and lessons
What the labels mean
- Free lesson, Pro answers
- the whole lesson is free; the model answers to its interview questions open with a pass
- Pro
- opens with a Pro, Premium or Ultimate pass
Minutes are reading time.
What a pass opens in this track
153 interview answers. The same pass opens every other track and every Pro tool.
- Module 1
Start here: the shell, the terminal and your first script
What a terminal and a shell are, which Bash you have, how commands, options and help work, moving around files, and writing and running a first script.
- Shells, terminals and which Bash you have Free lesson, Pro answers: 5 minutes
- Commands, options, arguments and help Free lesson, Pro answers: 5 minutes
- Files and directories from the command line Free lesson, Pro answers: 5 minutes
- Your first script: shebang, chmod and running it Free lesson, Pro answers: 5 minutes
- Module 2
How Bash reads a command line
The order in which Bash parses and expands a command line: quoting, word splitting, globbing, brace and tilde expansion, and printing with printf and echo.
- How Bash reads a line: tokens and expansions Free lesson, Pro answers: 5 minutes
- Quoting: single, double, backslash and $'...' Free lesson, Pro answers: 5 minutes
- Word splitting and IFS: why "$var" needs quotes Free lesson, Pro answers: 5 minutes
- Globbing: wildcards, dotfiles, extglob, globstar Free lesson, Pro answers: 5 minutes
- Brace and tilde expansion Free lesson, Pro answers: 5 minutes
- printf and echo: printing text reliably Free lesson, Pro answers: 5 minutes
- Module 3
Variables, parameters and arithmetic
Shell and environment variables, positional and special parameters, parameter expansion for defaults and strings, namerefs and integer arithmetic.
- Variables, export and the environment Free lesson, Pro answers: 5 minutes
- Positional and special parameters Free lesson, Pro answers: 5 minutes
- Defaults and required values: ${v:-x}, ${v:?} Free lesson, Pro answers: 5 minutes
- String operations with parameter expansion Free lesson, Pro answers: 5 minutes
- Indirection, namerefs and ${var@Q} transforms Free lesson, Pro answers: 5 minutes
- Integer arithmetic: $(( )), (( )) and let Free lesson, Pro answers: 5 minutes
- Module 4
Exit status, tests and branching
Exit statuses and command lists, test and [ ], [[ ]] conditions, and branching with if, elif, else and case.
- Exit status, &&, || and command lists Free lesson, Pro answers: 5 minutes
- test and [ ]: file, string and number checks Free lesson, Pro answers: 5 minutes
- [[ ]]: patterns, regex and BASH_REMATCH Free lesson, Pro answers: 5 minutes
- if, elif, else and case statements Free lesson, Pro answers: 5 minutes
- Module 5
Loops
for, while and until loops, break and continue, and reading files and command output line by line without losing data.
- for loops: lists, globs, ranges and C-style Free lesson, Pro answers: 10 minutes
- while, until, break, continue and select Free lesson, Pro answers: 10 minutes
- Reading files line by line with read Free lesson, Pro answers: 10 minutes
- Module 6
Redirection, pipes and substitution
Redirecting standard input, output and error, pipelines and pipefail, here-documents, file descriptors with exec, and command and process substitution.
- Redirection: stdin, stdout, stderr, /dev/null Free lesson, Pro answers: 15 minutes
- Pipelines, PIPESTATUS and pipefail Free lesson, Pro answers: 10 minutes
- Here-documents and here-strings Free lesson, Pro answers: 10 minutes
- File descriptors and exec redirections Free lesson, Pro answers: 10 minutes
- Command and process substitution Free lesson, Pro answers: 10 minutes
- Module 7
Functions and script structure
Writing functions, returning values and statuses, local variables, sourcing libraries and laying out a script so it is easy to read and change.
- Functions: arguments, local and return status Free lesson, Pro answers: 10 minutes
- Returning values from functions Free lesson, Pro answers: 15 minutes
- Script structure: main, source and libraries Free lesson, Pro answers: 10 minutes
- Module 8
Arrays and lists
Indexed and associative arrays, and using arrays to build commands and lists safely without word-splitting bugs.
- Indexed arrays: create, slice and loop Free lesson, Pro answers: 10 minutes
- Associative arrays: key-value maps Free lesson, Pro answers: 10 minutes
- Lists done right: arrays, mapfile and read -a Free lesson, Pro answers: 15 minutes
- Module 9
Robust scripts: errors, traps and debugging
set -e, set -u and pipefail with their limits, traps and cleanup, error-handling patterns, and debugging scripts with xtrace and other tools.
- set -e, set -u and pipefail: what they really do Free lesson, Pro answers: 10 minutes
- Traps and cleanup: EXIT, ERR and signals Free lesson, Pro answers: 10 minutes
- Error-handling patterns for scripts Free lesson, Pro answers: 10 minutes
- Debugging: bash -x, PS4, trap DEBUG and logs Free lesson, Pro answers: 10 minutes
- Module 11
Text processing: the Unix toolbox
The Unix text toolbox from the shell: sort, uniq, grep, regular expressions, sed, awk, find and xargs, jq for JSON, CSV and TSV, dates and numbers.
- cut, tr, head, tail, wc, paste and friends Free lesson, Pro answers: 5 minutes
- sort and uniq: counting and top-N reports Free lesson, Pro answers: 10 minutes
- grep: searching text and files Free lesson, Pro answers: 10 minutes
- Regular expressions for grep, sed and awk Free lesson, Pro answers: 10 minutes
- sed: substitute, delete and print Free lesson, Pro answers: 10 minutes
- sed beyond s///: hold space and multiline Free lesson, Pro answers: 25 minutes
- awk: fields, patterns and actions Free lesson, Pro answers: 15 minutes
- awk programs: arrays, joins and reports Free lesson, Pro answers: 10 minutes
- find and xargs: working with many files Free lesson, Pro answers: 15 minutes
- jq: query and reshape JSON from the shell Free lesson, Pro answers: 15 minutes
- CSV, TSV and YAML from the shell Free lesson, Pro answers: 10 minutes
- Dates, times and non-integer numbers Free lesson, Pro answers: 20 minutes
- Module 12
Processes, jobs and signals
Subshells and the execution environment, background jobs and wait, job control, signals, kill and timeout, named pipes and coprocesses.
- Subshells and the execution environment Free lesson, Pro answers: 10 minutes
- Background jobs, wait and parallel work Free lesson, Pro answers: 10 minutes
- Job control: Ctrl-Z, fg, bg, jobs, disown Free lesson, Pro answers: 10 minutes
- Signals, kill and timeout Free lesson, Pro answers: 15 minutes
- Named pipes and coprocesses Free lesson, Pro answers: 15 minutes
- Module 13
The interactive shell
Startup files and PATH, history and readline, the prompt, aliases and completion for a faster everyday shell.
- Startup files, PATH and login shells Free lesson, Pro answers: 5 minutes
- History, Ctrl-R and readline editing Free lesson, Pro answers: 5 minutes
- Prompt, aliases, dirstack, completion Free lesson, Pro answers: 5 minutes
- Module 14
Portability: POSIX sh, versions and platforms
POSIX sh versus Bash, the POSIX.1-2024 shell changes, GNU versus BSD tools on Linux and macOS, and checking Bash versions and features.
- POSIX sh vs Bash: spotting and fixing bashisms Free lesson, Pro answers: 15 minutes
- What POSIX.1-2024 changed for shell scripts Free lesson, Pro answers: 15 minutes
- GNU vs BSD tools: Linux and macOS differences Free lesson, Pro answers: 15 minutes
- Bash 3.2 to 5.3: versions and feature checks Free lesson, Pro answers: 10 minutes
- Module 15
Security for shell scripts
Avoiding injection through eval and untrusted input, and secure scripting practices for files, temporary paths, permissions and secrets.
- Injection, eval and untrusted input Free lesson, Pro answers: 15 minutes
- Secure scripting: temp files, secrets and PATH Free lesson, Pro answers: 15 minutes
- Module 16
Quality: ShellCheck, tests, style and speed
ShellCheck static analysis, testing scripts with Bats, style for maintainable scripts, and making scripts faster by avoiding needless forks.
- ShellCheck: catch bugs before a script runs Free lesson, Pro answers: 10 minutes
- Testing Bash scripts with Bats Free lesson, Pro answers: 10 minutes
- Style and maintainable scripts Free lesson, Pro answers: 10 minutes
- Fast Bash: builtins, forks and measuring Free lesson, Pro answers: 15 minutes
- Module 17
Scripts in the real world
Scheduled jobs with cron, shell in CI, containers, make and hooks, logging, locking and idempotent scripts, archives, backups and remote work.
- cron: scheduling scripts that actually run Free lesson, Pro answers: 20 minutes
- Shell in CI, Docker, Makefiles and Git hooks Free lesson, Pro answers: 20 minutes
- Logging, locking and idempotent scripts Free lesson, Pro answers: 15 minutes
- Archives, checksums and backups Free lesson, Pro answers: 20 minutes
- Remote work: ssh, rsync and curl in scripts Free lesson, Pro answers: 20 minutes
- Module 18
Choosing tools and interview preparation
When Bash is the right tool and when another language fits better, and preparing for shell scripting interview questions.
- When to use Bash, and when to switch to Python Free lesson, Pro answers: 20 minutes
- Shell scripting interview questions, explained Free lesson, Pro answers: 15 minutes
Related tools
Official documentation
- Bash Reference Manual (Chet Ramey (GNU Bash maintainer))
- POSIX.1-2024, Shell Command Language (The Open Group and IEEE)
- ShellCheck wiki (ShellCheck)