Scripting & Automation

Bash Scripting Essentials

Write robust shell scripts with proper error handling.

40 min read beginner 3 objectives

Status

Not started

What you will learn

  • Structure a safe bash script
  • Use set -euo pipefail
  • Handle arguments

New to this? Start here

The basics, in plain English

A script is just a text file containing a list of commands, saved so you can run them all at once instead of typing each one by hand. Bash is the most common language for these scripts on Linux.

Script
A file full of commands that run top to bottom, automating something you would otherwise type.
Bash
A common shell and scripting language on Linux. The thing reading your commands.
Shebang ( #! )
The first line of a script that tells the system which program should run it.
Argument
Extra input you pass to a script, like a filename, written after its name.
set -e
A safety switch that stops the script immediately if any command fails.
Why script
So a long, error-prone manual task becomes one reliable, repeatable command.
01

Safety first

Start every script with set -euo pipefail so it fails loudly instead of silently corrupting things. Quote your variables.

Try it yourself

bash
$#!/usr/bin/env bash
Shebang line: run this script with bash.
$set -euo pipefail
Fail fast on errors, unset variables, and broken pipes.
$name="${1:?usage: deploy <name>}"
Require a first argument or exit with a usage message.
$echo "deploying ${name}"
Print what we are about to deploy.
↳ lines explain what each command does — only the commands get copied

Finished this topic?

Mark it done to earn 100 XP and keep your streak alive.