Linux Tutorial

Bash Scripting and Troubleshooting

Automate repeatable tasks with variables, conditions, loops, exit codes and defensive scripting.

Concept

Shell scripts are ideal for connecting command-line tools and automating system tasks. Quote variables unless you intentionally want word splitting or glob expansion.

Exit codes communicate success or failure. For operational scripts, validate inputs, handle errors deliberately and log enough context to diagnose failures.

Example

#!/usr/bin/env bash
set -euo pipefail
name="Mango Learner"
for item in linux git docker; do
  printf '%s: %s\n' "" ""
done
Type the example yourself and change at least one value. Small experiments reveal syntax and behavior faster than passive reading.

Practice Tasks

  1. Write a script that creates a dated backup directory.
  2. Validate that one command-line argument is provided.
  3. Loop over .log files and print their sizes.

Key Takeaways

  • Quote variable expansions.
  • Check inputs and exit codes.
  • Keep automation repeatable and observable.