A shell script is just a file of commands you'd type anyway — the fastest way to automate backups, deploys, and chores on Linux.
A safe skeleton
#!/usr/bin/env bash
set -euo pipefail # exit on error, undefined var, or failed pipe
name="${1:-world}" # first arg, default "world"
echo "Hello, $name"
for f in *.log; do
[ -e "$f" ] || continue
gzip "$f"
done
The building blocks
| Piece | Example |
|---|---|
| Variables | x=5 · use "$x" (quote it!) |
| Conditionals | if [ -f file ]; then … fi |
| Loops | for, while |
| Functions | do_thing() { … } |
| Exit codes | cmd && echo ok · $? |
The rules that save you
Always
set -euo pipefailand always quote your variables ("$var"). Unquoted variables and silent failures cause the scariest bugs — a script that deletes the wrong thing because a variable was empty.
For anything complex, reach for Python — but for gluing commands together, Bash is unbeatable. Run scripts on a schedule with Cron and Scheduling.
Related: Cron and Scheduling · Understanding systemd · File Permissions Explained