DevOps and cloud

Stage 1 · Lesson 7 of 10

Writing Bash scripts

Linux, the shell and scripting

A is a shell script: a text file of commands the shell runs from top to bottom. It starts with a #!/usr/bin/env bash line and is made runnable with chmod +x. Learn variables, quoting, if tests, for loops, functions, (a number saying whether a command worked) and script arguments.

Start every script with set -euo pipefail. It stops the script at the first failed command, at an unset variable, or when any command inside a pipe fails. Quote your variables ("$file", not $file) so file names with spaces do not break things.

Run on everything you write; it catches common mistakes. Scripts automate backups, and deploy steps. The same shell lines later end up inside CI pipelines and .

Common trapletting a script grow to hundreds of lines; when it needs real data structures or error handling, rewrite it in Python.

A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock

Resources for this stage

Start with· follow one of these

Practise

Keep handy

  • The handful of flags people actually use for each command, one line each

Also worth your time

Mark this stage as not done?