🤖 Terminal Simulator — Module 6

Bash Scripting & Automation Basics

Stop running commands manually. Build a backup script from scratch, schedule it with cron, and never SSH in at 2 AM again.

Module Progress0/8 steps
STEP 1 / 8
echo + >

Birth of a Script — The Shebang Line

Real-World Scenario

It's Friday afternoon. Your manager says: "We need daily backups of all log files to /var/backups. Can you automate it?" Instead of running 15 commands every day, you'll write ONE script that does everything. Step one: create the file and tell Linux which interpreter to use. The shebang `#!/bin/bash` on line 1 is MANDATORY — without it, Linux doesn't know this is a bash script.

Technical Breakdown

The shebang `#!` (hash-bang) on line 1 tells the kernel which interpreter to use when executing the file. `#!/bin/bash` means "run this with bash." Without it, `./script.sh` may fail or use the wrong shell. Other shebangs: `#!/usr/bin/env python3` for Python, `#!/bin/sh` for POSIX shell. `echo '#!/bin/bash' > backup.sh` creates the file and writes the shebang as the first line. The single quotes around `#!/bin/bash` prevent bash from interpreting `!` as history expansion.

#!/bin/bashShebang — tells Linux to execute this file with bash.
#!/bin/shPOSIX shell — more portable, fewer features than bash.
#!/usr/bin/env bashPortable shebang — finds bash in $PATH (recommended).
set -eExit immediately if any command fails (best practice).
set -xDebug mode — print each command before executing it.

Your Task

Create the backup script with a shebang. Type: echo '#!/bin/bash' > backup.sh

devops@prod-server-03 — bash
devops@prod-server:~$

Quick Guide: Bash Scripting

Understanding the basics in 30 seconds

How It Works

  • #!/bin/bash (shebang) tells Linux which interpreter to use
  • VAR="value" assigns variables — access with $VAR (no spaces around =)
  • mkdir -p creates directories idempotently — safe to run repeatedly
  • for f in *.log; do ... done iterates over all matching files
  • echo "message" adds logging — silent scripts are dangerous
  • chmod +x makes the script executable — required for ./ execution
  • ./script.sh runs it — ./ prefix is mandatory (security feature)
  • crontab -e schedules it — 0 2 * * * means daily at 2:00 AM

Key Benefits

  • Automate repetitive tasks that take 15+ minutes manually
  • Build idempotent scripts that are safe to run multiple times
  • Add proper logging for debugging and audit trails
  • Schedule tasks with cron — no more 2 AM SSH sessions
  • Use variables for maintainability — change one line, not twenty
  • Foundation for CI/CD pipelines and infrastructure automation

Real-World Uses

  • Automated daily log backup and rotation
  • Database dump scripts scheduled via cron
  • Deployment scripts that build, test, and ship code
  • Health check scripts that alert when services go down
  • Cleanup scripts that remove old containers, images, temp files
  • Configuration management across multiple servers

The Bash Scripting Playbook

The Anatomy of a Production Script

Every production bash script follows the same skeleton. Here's the template that senior engineers use as a starting point:

#!/bin/bash
set -euo pipefail# Exit on error, undefined vars, pipe fails

TARGET_DIR="/var/backups"# Variables at the top
LOG_FILE="/var/log/backup.log"

mkdir -p "$TARGET_DIR"# Idempotent setup

for f in *.log; do# Process files
  cp "$f" "$TARGET_DIR/"
done

echo "Done at $(date)"# Always log completion

🛡️ Best Practices

  • set -e — Exit on any error
  • set -u — Error on undefined variables
  • Always quote "$variables"
  • Use UPPER_CASE for constants
  • Add echo statements for logging

⏰ Cron Cheat Sheet

  • * * * * * — Every minute
  • */5 * * * * — Every 5 minutes
  • 0 2 * * * — Daily at 2:00 AM
  • 0 0 * * 0 — Weekly (Sunday midnight)
  • 0 0 1 * * — Monthly (1st at midnight)

⚠️ Common Bash Gotchas

  • ❌ VAR = value → spaces cause "command not found". Use VAR=value
  • ❌ if[$x == $y] → missing spaces. Use if [ "$x" == "$y" ]
  • ❌ Unquoted $var with spaces → word splitting breaks paths
  • ❌ Missing shebang → script runs in wrong shell with wrong syntax
  • ❌ Relative paths in cron → cron runs in minimal environment without $PATH

The Infinity

Weekly tech insights, programming tutorials, and the latest in software development. Join our community of developers and tech enthusiasts.

Connect With Us

Daily.dev

Follow us for the latest tech insights and updates

Š 2026 The Infinity. All rights reserved.