Bash/Shell Scripting

Bash is the default shell on most Linux systems and macOS, and the glue of automation everywhere — DevOps workflows, CI/CD pipelines, container entrypoints, and system administration. A little Bash fluency goes a long way toward operating servers and automating repetitive work.

Bash is wonderful for orchestrating commands and small automation, but it's easy to write fragile scripts. The antidotes are simple: always quote variables, use strict mode (set -euo pipefail), run ShellCheck, and reach for Python or Go once logic gets complex.

TL;DR

Quick Example

A safe script skeleton — strict mode plus a cleanup trap:

Core Concepts

Variables & special variables

Conditionals

Common test operators: strings == != -z (empty) -n (non-empty); numbers -eq -ne -lt -le -gt -ge; files -e (exists) -f (file) -d (dir) -r/-w/-x; logic && || !.

Loops

Functions

Arrays

String manipulation

Input & redirection

Error Handling & Safety

Command-Line Arguments

For long options (--verbose, --file), loop over "$@" with a case and shift/shift 2.

Process Management

Practical Examples

Best Practices

Common Mistakes

Unquoted variables

Ignoring failures

When NOT to Use Bash

Essential Tools

Classic: grep, sed, awk, cut, sort, uniq, find, xargs, rsync, tar, curl, jq. Modern alternatives: ripgrep (grep), fd (find), bat (cat), eza (ls), jq/yq (JSON/YAML).

FAQ

When should I use Bash vs Python?

Bash excels at gluing commands together — running tools, piping data, and simple automation. Once you need data structures, complex logic, error handling, or maintainability, switch to Python or Go. A rough rule: if the script grows past ~100 lines or needs real logic, it's outgrown Bash.

What does set -euo pipefail do?

-e exits on any command failure, -u errors on undefined variables, and -o pipefail makes a pipeline fail if any command in it fails. Together they turn silent failures into loud, early ones — the single highest-value habit for robust scripts.

Why must I quote variables?

Unquoted variables undergo word splitting and glob expansion, so rm $file breaks on filenames with spaces and $list splits unexpectedly. Quoting ("$file", "$@") preserves values exactly and prevents a whole class of bugs and security issues.

What is ShellCheck?

A static analyzer for shell scripts that catches quoting bugs, undefined variables, and common pitfalls. Run it locally and in CI — it catches the mistakes that make Bash scripts fragile, and explains each finding.

Related Topics

References