Bash Scripting & Command Line Automation

Pipes, Filters, Shell Variables, Cron Jobs & Defensive Scripting

8 modules, 40 lessons: The Shell And The Command Line; Quoting And Word Splitting; Redirection, Pipelines And Text; Variables And Expansion; Writing Scripts; Functions And Scripts That Do Not Break; Debugging, Style And Portability; The Shell As A System Tool.

Beginner · 4 hours · 40 lessons · 235 labs

Who it is for

Beginner level.

For anyone who types commands and wants the computer to repeat the work for them: support and operations staff, analysts, testers and new engineers.

What you don't need yet:

What helps:

Before you start:

Take first:

Take next:

Learning paths:

Nobody needs to know everything before they start. Each course builds on the one before, so a learning path takes you there step by step.

Labs

235 labs, 111 of them graded: you write the code, it runs, and graded labs check the result. The outline lists each lesson's labs.

What is free

Start free: First module free with an account. Pro: Every course, lab, handbook, practice exam and certificate.

Course outline

The Shell And The Command Line

  1. What A Shell Is, And What Bash Is (3 labs): You will know the difference between a terminal and a shell, and what Bash is.
  2. Which Shell Am I In, And Getting Bash Five (7 labs): You will be able to check your current shell and its version, and know how to get bash five.
  3. Moving Around: pwd, ls, cd And Paths (6 labs): You will be able to navigate the file system and understand absolute and relative paths.
  4. Making, Copying, Moving And Removing (8 labs): You will be able to create, copy, rename, and delete files and directories.
  5. Help, History, Completion And Aliases (8 labs): You will be able to read manuals, use command history, auto complete paths, and define aliases.

Quoting And Word Splitting

  1. What Bash Does To A Line Before It Runs It (4 labs): You will be able to name the order of the shell expansions and predict how a line is rewritten before it runs.
  2. Single Quotes, Double Quotes, Backslash (5 labs): You will be able to choose between single quotes, double quotes and a backslash, and get a single quote inside a single quoted string.
  3. The Unquoted Variable: Splitting And Globbing (5 labs): You will be able to recognise the failures an unquoted variable causes and quote expansions so they cannot happen.
  4. The Field Separator, And Reading Lines Safely (5 labs): You will be able to control word splitting with the field separator and read a file line by line without losing anything.
  5. Arrays, And Why Dollar At Is Not Dollar Star (4 labs): You will be able to build and expand arrays and forward a script's arguments without damaging them.

Redirection, Pipelines And Text

  1. Standard Input, Output And Error (4 labs): Understand the three standard streams and how commands use them to read and write data.
  2. Redirection: Truncate, Append, Both Streams (4 labs): You will be able to capture output and errors separately or together, discard them, and avoid destroying the file you are reading.
  3. Pipelines, tee And Process Substitution (6 labs): You will be able to chain commands with a pipeline, keep a copy of the data mid-pipeline with tee, and feed a command a file name that is really another command.
  4. Finding Lines: grep And Regular Expressions (5 labs): You will be able to search files and whole trees with grep, and describe what you are looking for with a regular expression.
  5. Reshaping Text: cut, sort, uniq, tr, sed, awk (5 labs): You will be able to slice columns, order rows, count repeats, translate characters and summarise fields with the standard text tools.

Variables And Expansion

  1. Shell Variables, The Environment And export (4 labs): Define shell variables, mark them for export to child processes, and explain the difference between local shell variables and the environment.
  2. Parameter Expansion: Defaults, Length, Slices (5 labs): Use parameter expansion to provide default values, measure string length, and extract slices of a string.
  3. Trimming, Replacing And Changing Case (6 labs): Use parameter expansion to trim prefixes and suffixes, replace text, and change the case of strings.
  4. Command Substitution And Subshells (5 labs): Capture the output of a command using command substitution, and explain how subshells isolate state changes like working directories and variables.
  5. Arithmetic: Double Parentheses, let And bc (5 labs): Perform integer math using double parentheses and the let command, and calculate floating point numbers using the basic calculator.

Writing Scripts

  1. Shebangs, Permissions And Three Ways To Run (4 labs): Write a script with a shebang, make it executable, and run it directly or by passing it to bash.
  2. Exit Codes, And What Success Means (7 labs): You will be able to read and set the exit status of a command to control script execution.
  3. Conditionals: test, Brackets, Double Brackets, case (6 labs): Use if statements with test, single brackets, and double brackets to branch logic, and write case statements for multiple choices.
  4. Loops: for, while, until, break, continue (11 labs): Write loops to repeat commands, and use break and continue to control them.
  5. Script Input And Output: read, Heredocs, printf (7 labs): Read user input interactively, read file lines in a loop, write multi-line text cleanly, and format variables predictably.

Functions And Scripts That Do Not Break

  1. Functions, local And declare (12 labs): Write reusable shell functions that do not leak variables into the global scope.
  2. Arguments: Dollar One, Dollar At, shift (7 labs): Read a script's arguments, pass them on intact, and walk through them with shift.
  3. Option Parsing With getopts (6 labs): Parse single letter flags and options that carry a value with the getopts builtin.
  4. set -e, set -u And pipefail: Promises And Traps (6 labs): Turn on the three safety options and recognise the failures they still let through.
  5. trap, Cleanup And Temporary Files (7 labs): Clean up reliably on the way out using trap, a temporary directory and an idempotent cleanup function.

Debugging, Style And Portability

  1. Debugging: set -x, bash -n And Reading The Error (6 labs): Check a script without running it, trace what it really does, and read what a bash error message is telling you.
  2. ShellCheck (7 labs): Run ShellCheck over a script, read a finding by its code and severity, and silence one line with a reason.
  3. Style That Prevents Bugs (6 labs): Write shell the way the Google style guide does, and recognise the size at which a script should become a program in another language.
  4. POSIX sh Versus Bash (7 labs): Tell a bashism from portable shell, and know which program will read your script when the shebang says sh.
  5. eval, And What To Use Instead (6 labs): Recognise eval as a code injection hole and replace it with an array, an indirect expansion or a name reference.

The Shell As A System Tool

  1. Processes, Jobs And Signals (5 labs): You will be able to list processes, move jobs between the foreground and the background, and send named signals to stop them.
  2. Permissions And Ownership (6 labs): You will be able to read a permission string, set modes in both the symbolic and the numeric form, and explain what a umask and a directory execute bit do.
  3. find And xargs (5 labs): You will be able to select files by name, type, depth and emptiness with find, and hand the results to another command without a file name ever breaking it.
  4. Archives, Transfers And The Network (5 labs): You will be able to build, inspect and unpack a compressed archive, and know which command to reach for when a script has to copy or fetch something over the network.
  5. Scheduling, Monitoring And A Script That Reports (5 labs): You will be able to read and write a crontab line, and build the report script this course leaves behind for the ones that follow it.

Certification courses