Script Basics & Variables

Anatomy of a Script

#!/bin/bash                 # shebang - which interpreter
set -euo pipefail          # fail fast on errors & unset vars

name="world"               # no spaces around =
echo "Hello, $name"

Make it executable and run it: chmod +x script.sh && ./script.sh.

VariableMeaning
$0Script name
$1, $2Positional arguments
$#Number of arguments
$@All arguments (as separate words)
$?Exit status of last command
$Current PID

Command substitution: today=$(date +%F). Arithmetic: count=$((count + 1)).

Exam tip: set -e exits on any command failure, set -u errors on unset variables, set -o pipefail makes a pipeline fail if any stage fails. Together they make scripts safe.