Scripts

A file of commands, with a first line naming its interpreter

backup-etc#!/usr/bin/env bashthe first line names the interpreterthen one command per line$ chmod +x backup-etcmark it executable, once$ ./backup-etcthe kernel reads #! and runs: /usr/bin/env bash ./backup-etc$ bash backup-etcthe same, without needing the x bit# cp backup-etc /usr/local/bin/now on PATH: run it as backup-etc from anywhere$ echo $?0every command, and every script, ends with an exit status: 0 for success, 1 to 255 for failureA script is a text file of commands; the #! line says which program runs it.Anything you can type at the prompt can go in a script, and it runs the same way. Scripts save typing, but mostly they make a taskthe same every time, at 3 a.m. as at 3 p.m. #!/usr/bin/env bash finds bash on PATH; #!/bin/bash names it directly. Both are common.

A script is a text file of commands; the shebang line names the interpreter and chmod +x makes it runnable.

Expansion

Before a line runs, bash fills in every $

You writeBash turns it intoWhyname=ana(sets name)a variable: no spaces round the ="hi $name"hi anavariables expand in double quotes$(date +%A)Sundaycommand substitution: the output$((2 + 3))5arithmetic, whole numbers${1:-/etc}/etcargument 1, or a default if missing${p##*/}access.logp=/var/log/nginx/access.log: cut up to the last /${p%/*}/var/log/nginxcut from the last / to the end'hi $name'hi $namesingle quotes: nothing expandsBefore a command runs, bash replaces each $ form with a value: variables, command output, arithmetic.This is expansion, and it happens on every line. Variables are text: name=ana, never name = ana, which runs a command called name.Inside double quotes everything expands; inside single quotes nothing does. Every result here was printed by bash 5.2.

Bash expands variables, command substitution and arithmetic before running a line; single quotes stop it.

Arrays · Going deeper

A list of items, each kept whole

files=("$dest"/etc-*.tar.gz)a glob fills an array, one item per file, sorted by nameetc-2026-11-08-0230.tar.gz[0]etc-2026-11-09-0230.tar.gz[1]…etc-2026-11-15-1027.tar.gz[7]${#files[@]}8how many items${files[0]}etc-2026-11-08-0230.tar.gzone item, counted from 0: the oldest"${files[@]}"every item, each kept wholeuse this form in loops: for f in "${files[@]}"files+=(new.tar.gz)adds one at the enddeclare -A port=([ssh]=22)an associative array: ${port[ssh]} is 22An array holds a list of items, each kept whole, even names with spaces.A plain variable holds one string, and splitting it back into items breaks on spaces. An array never needs splitting. Dates writtenyear-month-day sort correctly as text, which is why the backup script can treat the first items as the oldest.

An array holds many values; "${files[@]}" gives each one intact, ${#files[@]} counts them.

Quoting

An unquoted variable splits at spaces; quotes keep it whole

file="my report.txt"one variable, one name with a space in it$ rm $filethe value is split at the spacerm gets two arguments:myreport.txtdeletes the wrong files, or fails$ rm "$file"double quotes keep it wholerm gets one argument:my report.txtsee it for yourself$ printf '[%s]\n' $file[my][report.txt]$ printf '[%s]\n' "$file"[my report.txt]The rulePut double quotes round every $variableand $(command), unless you want splitting.Inside [[ ]] quotes are optional; everywhereelse, missing quotes are the top bug.Unquoted variables are split at spaces and expanded as wildcards; quote them.After expansion, bash splits unquoted results into separate words, then matches any * or ? against file names. A name with a spacebecomes two names. "$var" keeps the value as one argument, whatever it holds. ShellCheck flags every unquoted use (SC2086).

Unquoted expansions undergo word splitting and globbing; "$var" always stays one argument.

Exit codes and tests

if runs a command and acts on its exit status

$ grep -q root /etc/passwd; echo $?0$ grep -q nosuchuser /etc/passwd; echo $?1$ ls /nope 2>/dev/null; echo $?2if runs a command and checks its exit statusif grep -q "^$user:" /etc/passwd; then echo "$user exists"else useradd "$user"fimkdir -p "$d" && cd "$d" || exit 1&& runs next on success, || on failureTestTrue whenTestTrue when[[ -f $p ]]a regular file[[ -z $s ]]the string is empty[[ -d $p ]]a directory[[ $a == "$b" ]]strings are equal[[ -x $p ]]executable[[ $h =~ ^web[0-9]+ ]]matches the regex(( n > 5 ))numbers compare[[ $n -gt 5 ]]the same, older styleEvery command returns an exit status, 0 for success; if, && and || act on it.if does not test true or false: it runs a command and looks at its status. [[ ]] is a command that tests files, strings and patterns;(( )) compares numbers. Use them in your own scripts: exit 0 when all went well, and a different number for each kind of failure.

Every command returns an exit status, 0 for success; if, && and || act on it, and and (( )) are the test commands.

Loops

Repeat for each item, each line or each argument

for each item in a listfor h in web01 web02 db01; do ssh "$h" uptimedonefor f in /var/log/*.log; doa glob: one file per passfor each line of inputwhile IFS= read -r user; do useradd "$user"done < new-users.txtIFS= and -r keep each line exactlyas written: spaces and backslasheschoose by patterncase $1 in start|stop) svc "$1" ;; *) echo "usage" >&2 exit 2 ;;esacArguments: ./deploy web01 v2.4$0$1$2$#"$@"./deployweb01v2.42all arguments, each kept wholeshift drops $1 and moves the rest down; getopts reads options such as -v and -n 3.Loops repeat a command for each item, each line or each argument; case picks code by pattern.for walks a list or a glob; while read walks a file line by line; case matches one value against patterns. $1, $2 and "$@" arethe script’s arguments, and $# says how many there are, so a script can check it was called correctly before doing anything.

for walks a list or glob, while read walks lines, case matches patterns; $1, $# and "$@" are the script’s arguments.

A real script

A backup script, line by line

1 #!/usr/bin/env bash 2 # backup-etc: archive a directory and keep the newest copies 3 set -euo pipefail 4 5 src=${1:-/etc} 6 dest=${BACKUP_DIR:-/var/backups/etc} 7 keep=7 8 archive="$dest/etc-$(date +%F-%H%M).tar.gz" 9 10 log() { echo "$(date +%T) $*" >&2; }11 trap 'log "failed at line $LINENO"' ERR12 13 [[ -d $src ]] || { log "no such directory: $src"; exit 2; }14 mkdir -p "$dest"15 16 log "archiving $src to $archive"17 tar -czf "$archive" -C / "${src#/}"18 19 files=("$dest"/etc-*.tar.gz) # oldest first20 excess=$(( ${#files[@]} - keep ))21 for (( i = 0; i < excess; i++ )); do22 log "removing ${files[i]}"23 rm -- "${files[i]}"24 done25 log "done: $(du -h "$archive" | cut -f1)"1Run with bash1found on PATH by env2Stop on any failure2errors, unset names, broken pipes3Settings with defaults3argument 1, or /etc; a variable, or a path4A function4messages go to stderr, with the time5Say where it failed5runs whenever a command fails6Check the input6exit code 2: wrong usage7Keep the newest 77an array of names, sorted by dateTwenty-five lines: settings first, then checks, then the work, with every failure stopping the run.

Settings with defaults, safety settings, a log function, an input check and an array that keeps the newest seven archives.

Failing safely

Make the script stop at the first failure, and say where

LineWithout itWith itset -ea failed command is ignoredthe script stops at the first failureset -ua misspelt $nmae is an empty stringstops: "nmae: unbound variable"set -o pipefailfalse | true reports 0: successfalse | true reports 1: the failuretrap … ERRit fails silentlyit says where: "failed at line 14"a real failure, caught# BACKUP_DIR=/proc/backups backup-etcmkdir: cannot create directory '/proc/backups': No such file or directory10:27:13 failed at line 14# echo $?1set -e has gaps: commands in if,after && or ||, and before ! do notstop the script. Check what mattersyourself, and exit with a clear code.set -euo pipefail turns silent failures into a stop, and a trap says where it happened.By default bash carries on after a failure, which is how a backup script reports success with no backup. These lines make it stop.Here mkdir failed on line 14, so the script stopped before tar, printed the line, and exited with status 1 for cron to see.

set -e stops on errors, set -u on unset variables, pipefail on failures inside pipes; an ERR trap reports the line.

Failing safely · Going deeper · 1 of 2

bash -x shows every command as it really ran

# bash -x /usr/local/bin/backup-etc (trimmed)+ src=/etc1+ dest=/var/backups/etc++ date +%F-%H%M+ archive=/var/backups/etc/etc-2026-11-15-1027.tar.gz2+ [[ -d /etc ]]+ tar -czf /var/backups/etc/etc-2026-11-15-1027.tar.gz -C / etc3+ files=("$dest"/etc-*.tar.gz)+ excess=14+ (( i = 0 ))+ (( i < excess ))10:27:09 removing /var/backups/etc/etc-2026-11-08-0230.tar.gz+ rm -- /var/backups/etc/etc-2026-11-08-0230.tar.gz+ (( i++ ))+ (( i < excess ))510:27:09 done: 952K1${1:-/etc} became /etcno argument was given2$(date) ran first++ marks a nested command3"${src#/}" became etctar stores paths without the /48 files − 7 kept = 1one pass of the loop5The test fails: loop endsthen the last log linebash -x prints every command after expansion, so you see what the script really ran.Seven old archives plus the new one made eight, so excess was 1 and only the oldest, 8 November, was removed. Trimmed: the logfunction’s own lines are hidden. Add set -x and set +x around one part of a script to trace only that part.

bash -x prints each command after expansion, which shows exactly what values the script used.

ShellCheck · Going deeper · 2 of 2

Catch the classic bugs before the script runs

bad.shfor f in $(ls *.log); do rm $fdoneif [ $count > 5 ]; then echo many; fi$ shellcheck bad.sh (trimmed)SC2045 (error): Iterating over ls output is fragile. Use globs.SC2086 (info): Double quote to prevent globbing and word splitting.SC2071 (error): > is for string comparisons. Use -gt instead.Fixed:for f in ./*.log; do rm -- "$f"; done(( count > 5 )) && echo manyThe backup script on the previous pages:$ shellcheck backup-etc && echo cleancleanShellCheck reads a script and points out the classic bugs before you run it.Each warning has a code and a wiki page explaining it. [ $count > 5 ] is a real trap: inside [ ], > redirects to a file called 5.Install it from the distribution (shellcheck; on RHEL from EPEL) and run it on every script, or in your editor as you type.

ShellCheck reads a script and names its bugs, such as unquoted variables and parsing ls, with a fix for each.

Scheduling

Five fields say when; cron runs it with no one watching

30minute 0-592hour 0-23*day of month 1-31*month 1-12*day of week 0-7, Sunday is 0 or 7/usr/local/bin/backup-etcevery day at 02:30Where it goes$ crontab -eyour own table; crontab -l lists it/etc/cron.d/backupsystem file, adds a user field: 30 2 * * * root /usr/local/bin/backup-etcCron runs with almost nothing: PATH=/usr/bin:/bin, no terminal, your home as the directory. Give full paths, and send output to a log:30 2 * * * /usr/local/bin/backup-etc >> /var/log/backup-etc.log 2>&1Five fields say when, the rest says what: cron runs it, with a bare environment and no one watching.*/15 means every 15; 1-5 means Monday to Friday; 0 9 1 * * is 9:00 on the first of each month. at runs a command once (at 23:00);a systemd timer (lesson 8) does the same as cron with logs in the journal and catch-up after downtime. Check results in the log, not by hope.

A crontab line has five time fields, minute, hour, day, month and weekday, then the command; give full paths and log the output.

Lab

Write, break and schedule a backup script

$ vi backup-etc; chmod +x backup-etc1$ shellcheck backup-etc && echo clean2clean# ./backup-etc310:26:42 archiving /etc to /var/backups/etc/etc-2026-11-15-1026.tar.gz10:26:42 done: 952K# ./backup-etc /nope; echo $?410:26:42 no such directory: /nope2# echo "30 2 * * * root /usr/local/bin/backup-etc" > /etc/cron.d/backup51Write itfrom the pages, by hand2Check itbefore it runs3Run itand read the log lines4Break ita clear error and code 25Schedule itand check the next morningLab: write the backup script yourself, break it on purpose, then let cron run it every night.Any Linux VM works. Press the down arrow for the steps.

Write the backup script by hand, check it with ShellCheck, trace it, break it, extend it, and schedule it with cron.

Lab · Going deeper

Write, break and schedule a backup script

Type the script from the annotated page rather than pasting it: you learn more from your own typos. Work as root, or with sudo, because it reads /etc.

Steps

  1. Warm up. At the prompt: name=you; echo "hi $name"; echo 'hi $name'; f="a b"; touch "$f"; ls -l $f; ls -l "$f". Explain each result.
  2. Write it. Create backup-etc with the lines from the annotated page; chmod +x; run shellcheck on it until it is clean.
  3. Run it. sudo ./backup-etc; ls -l /var/backups/etc; tar -tzf on the new archive | head.
  4. Trace it. Make eight empty fake archives with older dates (touch /var/backups/etc/etc-2026-01-0{1..8}-0000.tar.gz), then sudo bash -x ./backup-etc and follow the loop.
  5. Break it. Run it with a missing directory and print $?. Then BACKUP_DIR=/proc/x and read the trap message. Remove set -e and watch it carry on.
  6. Add a feature. Accept a second argument for how many copies to keep, with a default of 7, and reject anything that is not a number: $2 =~ ^[0-9]+$ .
  7. Install it. Copy it to /usr/local/bin and add /etc/cron.d/backup that runs it every two minutes, with output appended to /var/log/backup-etc.log.
  8. Watch it. tail -f /var/log/backup-etc.log for a few minutes; journalctl -u cron (crond on RHEL) shows each start.
  9. Finish. Change the schedule to 02:30 daily. Optional: replace cron with a systemd service and timer, as in lesson 8.
Check: ls /var/backups/etc shows no more copies than you asked to keep, the log has a line from each run, and ./backup-etc /nope exits with status 2.

Lesson complete

You finished “Shell scripts”

Nicely done.

Open in a new tab