Bash Scripting

Variables, arrays, control flow, error handling, trap, and production-grade scripting patterns.

Overview

Bash is the dominant shell for system automation. Production scripts must be written with defensive defaults, because out of the box bash carries on after a failed command, expands a misspelled variable to nothing and hides failures inside pipelines.

Defensive Defaults

Start every script with one line that turns on three safety switches.

  • -e: exit on any non-zero return.
  • -u: treat unset variables as errors.
  • -o pipefail: fail a pipeline if any command in it fails.

Key concepts

  • set -euo pipefail
  • Double quoting
  • trap
  • Exit codes
  • Process substitution

Interview questions

  • Write a bash script that downloads a file, processes it, and cleans up temp files even if the script fails mid-way.
  • Explain the difference between $*, "$*", $@, and "$@" when passing arguments.

Sign up free to read the full topic

More Linux topics

Start free on Cariara · Coding problems · System design studio