Catalog My learning Blog
Linux Command Line Tutorial for Developers · Phase 2 · Lesson 15 · 5 min read

Bash Scripting Tutorial: Variables, If and Loops

On this page
  1. A bash script worth writing: a log checker
  2. Bash basics: arguments, variables, if and loops
  3. Bash functions and cleanup with trap
  4. Why you should quote your variables
  5. Bash strict mode: set -euo pipefail
  6. Checking scripts with shellcheck
  7. Check yourself

A bash script worth writing: a log checker

Checking which logs are too big by hand is tedious. A script that takes a directory and a size limit, reports each file, and exits non-zero when something's wrong can run in cron or CI:

Code
#!/usr/bin/env bash
# check-logs.sh <dir> [max_kb]: report log files larger than a limit
set -euo pipefail

if [[ $# -lt 1 ]]; then
  echo "usage: $0 <dir> [max_kb]" >&2
  exit 2
fi

dir=$1
max_kb=${2:-1}            # default 1 KB
big=0

for f in "$dir"/*.log; do
  size_kb=$(( $(stat -c %s "$f") / 1024 ))
  if (( size_kb >= max_kb )); then
    echo "BIG   $f (${size_kb} KB)"
    big=$((big + 1))
  else
    echo "ok    $f"
  fi
done

echo "$big file(s) at or over ${max_kb} KB"
(( big == 0 ))            # exit status: 0 if none are big

Run on a directory with three logs, one named with a space:

Shell
$ ./check-logs.sh logs; echo "exit $?"
ok    logs/app 1.log
BIG   logs/app2.log (1 KB)
ok    logs/db.log
1 file(s) at or over 1 KB
exit 1

$ ./check-logs.sh; echo "exit $?"
usage: ./check-logs.sh <dir> [max_kb]
exit 2

It passes shellcheck cleanly.

Bash basics: arguments, variables, if and loops

  • Shebang: #!/usr/bin/env bash runs it with bash wherever bash is installed.
  • Arguments: $1, $2, $# (count), $@ (all, keep it quoted as "$@"), $0 (script name).
  • Defaults: ${2:-1} uses 1 when the second argument is missing.
  • Arithmetic: $(( ... )) computes; (( ... )) tests numbers.
  • Command substitution: $(stat -c %s "$f") captures output.
  • Conditionals: [[ ... ]] for strings and files (-f file, -d dir, -z "$s" empty, ==), (( ... )) for numbers.
  • Loops: for f in "$dir"/*.log iterates over a glob; while read -r line; do ...; done < file reads lines.
  • Exit codes: 0 means success; anything else is failure, and callers (cron, CI, &&) depend on it.
  • Errors to stderr: >&2.
A script takes arguments, validates them, loops over inputs with quoted variables, branches on conditions, and exits with a status code that callers can act on; strict mode stops on errors.
A script takes arguments, validates them, loops over inputs with quoted variables, branches on conditions, and exits with a status code that callers can act on; strict mode stops on errors.

Bash functions and cleanup with trap

Once a script grows past a screen, split it into functions, and make sure temporary files are removed however the script ends:

Code
#!/usr/bin/env bash
set -euo pipefail

workdir=$(mktemp -d)
trap 'rm -rf "$workdir"; echo "cleaned up $workdir" >&2' EXIT   # runs however the script exits

log() { echo "[$(date +%H:%M:%S)] $*" >&2; }

count_errors() {
  local file=$1                     # local: doesn't leak into the rest of the script
  grep -c ERROR "$file" || true     # grep exits 1 when there are no matches
}

printf 'ok\nERROR disk\nERROR net\n' > "$workdir/app.log"
log "checking $workdir/app.log"
echo "errors: $(count_errors "$workdir/app.log")"
Code
[14:02:11] checking /tmp/tmp.XXXX/app.log
errors: 2
cleaned up /tmp/tmp.XXXX
  • A function's arguments are $1, $2 and so on, just like the script's, and local keeps its variables private.
  • trap '...' EXIT runs the cleanup on success, on an error under set -e, and on Ctrl+C, so no half-finished temp files are left behind.
  • || true matters in strict mode: when grep -c finds nothing it prints 0 but exits with 1, and without || true, set -e would stop the script there.

Why you should quote your variables

Code
f="logs/app 1.log"
ls -l $f
YAML
ls: cannot access 'logs/app': No such file or directory
ls: cannot access '1.log': No such file or directory

Unquoted, $f is split on the space into two arguments. ls -l "$f" works. Quote every variable expansion unless you specifically want splitting. This one habit prevents a whole category of bugs, including the classic rm -rf $DIR/ where an empty variable deletes from /.

Bash strict mode: set -euo pipefail

  • -e exits on the first failing command:
Shell
$ bash -c 'set -e; cp /nope /tmp/x; echo "never printed"'
cp: cannot stat '/nope': No such file or directory
exit 1
  • -u treats unset variables as errors, catching typos:
Shell
$ bash -c 'set -u; name=world; echo "hello $nmae"'
bash: line 1: nmae: unbound variable
  • -o pipefail fails a pipeline if any part fails (Day 8).

-e has gaps (it doesn't trigger inside conditions, or for commands before ||), so still check important commands yourself.

Checking scripts with shellcheck

shellcheck catches bugs before they run. On a two-line script with a common mistake, for f in $(ls logs); do rm $f; done:

Code
SC2045 (error): Iterating over ls output is fragile. Use globs.
SC2086 (info): Double quote to prevent globbing and word splitting.

Run it in your editor and in CI. When a script grows past 100 lines or needs data structures, consider Python.

Check yourself

The unquoted variable was split on the space into two separate arguments, logs/app and 1.log, neither of which exists. Writing "$f" keeps it as one argument.

References to unset variables, such as a misspelled $DEPOLY_DIR, which would otherwise silently expand to an empty string. That matters because a command like rm -rf "$DIR/"* with an empty variable can target the wrong place.

Its exit status becomes the script's: 0 when no files are too big, 1 otherwise, so cron or CI can react.

It runs the cleanup function whenever the script exits, normally or on error, so temporary files and locks are always removed.

[[ ]] is a bash keyword, so variables inside it aren't word-split or glob-expanded, which avoids many quoting bugs. It also supports pattern matching, regex with =~ and && inside the test. Use [ ] only for scripts that must run in plain sh.