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.
| Variable | Meaning |
|---|---|
$0 | Script name |
$1, $2 … | Positional 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 -eexits on any command failure,set -uerrors on unset variables,set -o pipefailmakes a pipeline fail if any stage fails. Together they make scripts safe.