Variables, Conditions & Loops
What is it?
Bash scripting glues commands with logic: variables hold values, conditions ([ ... ] / test) branch, and loops (for/while) repeat over lists or lines. It is the everyday automation language of Linux.
Why it matters
Repetitive admin work — over many files, hosts or log lines — is where mistakes and toil live. A small script does it consistently, every time, without a slip.
Where you see it
`name=value` (no spaces), `if [ -f /etc/x ]; then ...; fi`, `for f in *.log; do ...; done` — the building blocks of every ops script.
What normal looks like
Quoted variables ("$f"), explicit conditions, and loops that handle the empty case — a script that behaves the same on 0, 1 or 1000 items.
What suspicious looks like
Unquoted variables that break on spaces, an off-by-one condition, or a loop that does the wrong thing when the list is empty — subtle bugs that bite at scale.
How analysts investigate
Read a script by tracing one input through it, always quote variable expansions, and test on the tricky inputs (spaces, empty, many) before trusting it.
Common beginner mistakes
- Writing name = value with spaces (Bash treats it as a command).
- Leaving $var unquoted so a filename with a space splits into two.
Logic over commands
#!/usr/bin/env bash
threshold=90
usage=$(df / | awk 'NR==2{print $5}' | tr -d '%')
if [ "$usage" -ge "$threshold" ]; then
echo "WARN: root disk at ${usage}%"
fi
quote expansions; no spaces around =Quick check
Why quote "$f" in a loop over filenames?
A quick self-check — it doesn't affect your XP or progress.
Sign in to save your progress on the server.