Assignments
25 practical assignments
Do these on a disposable Ubuntu 24.04 VM or container (docker run -it --rm ubuntu:24.04 bash, then apt-get update && apt-get install -y shellcheck jq curl bats). Use Bash 5.x unless an assignment says otherwise. Every script you write should pass shellcheck with zero findings before you call it done. Open the hint only after you have tried for 10 minutes.
Beginner
A1. Three ways to run a script ๐ข
Objective: Understand the shebang, the execute bit, and the difference between running a script and sourcing it.
Tasks
- Write
whereami.shwith the shebang#!/usr/bin/env bashthat setsGREETING=hello, runscd /tmp, and prints$PWD,$GREETING,$0and$$. - Run it three ways:
./whereami.sh(afterchmod +x),bash whereami.sh, andsource whereami.sh. - After each run, print
$PWDand$GREETINGin your interactive shell and record what changed. - Change the shebang to
#!/bin/sh, add[[ -n $GREETING ]] && echo ok, and run it with./whereami.sh. Note the error on Ubuntu. - Remove the execute bit and show the exact error message for
./whereami.sh.
Expected outcome: A short table showing that only source changed your shell's directory and variables, $0 differs between the three methods, /bin/sh (dash) fails on [[, and a missing execute bit gives Permission denied.
Hints
./script uses the shebang and needs +x. bash script ignores the shebang. source runs the file in the current shell, so cd and variables persist, and $0 stays as your shell's name. On Ubuntu /bin/sh is dash, which prints [[: not found.
Bonus: Add a guard so the script refuses to be sourced: [[ ${BASH_SOURCE[0]} == "$0" ]] || { echo "do not source me" >&2; return 1; }.
A2. Break it, then quote it ๐ข
Objective: See word splitting and globbing destroy real filenames, then fix every expansion.
Tasks
- Create a test directory containing
report 2026.csv,*.txt(a file literally named with an asterisk),-rf, andnotes.txt. - Write
count_lines.shthat loopsfor f in $(ls)and runswc -l $f. Run it and save the errors. - Rewrite it with a glob (
for f in ./*), quoted expansions and--before filenames. - Use
printf '[%s]\n' $varandprintf '[%s]\n' "$var"to show how many words a value becomes in each case. - Run
shellcheckon both versions and list the warning codes the first one triggers.
Expected outcome: The fixed script prints a correct line count for all four files, including -rf and *.txt, and ShellCheck reports nothing.
Hints
$(ls) output is split on spaces and globbed, so report 2026.csv becomes two words and *.txt expands. Use for f in ./*; do wc -l -- "$f"; done. The ./ prefix (or --) stops -rf being read as options. Expect SC2045 (iterating over ls) and SC2086 (unquoted variable).
Bonus: Make the script handle an empty directory without running wc on a literal ./* (shopt -s nullglob).
A3. Parameter expansion instead of external tools ๐ข
Objective: Replace basename, dirname, sed and tr calls with parameter expansion.
Tasks
- Given
path=/var/log/nginx/access.log.2026-11-09.gz, print the filename, the directory, the name without.gz, the extension, and the date part, using only${...}forms. - Print
$pathwith every/replaced by_, and in upper case. - Write
config_value() { ... }that prints${1}if set and non-empty, otherwise a default passed as$2, using${var:-default}. - Make a script that aborts with a clear message if
ENVis unset, using${ENV:?...}. - Print the length of
$pathand the first 8 characters of it.
Expected outcome: Every value is printed with zero external commands (check with bash -x: no basename, sed or cut lines).
Hints
${path##*/} (filename), ${path%/*} (directory), ${path%.gz}, ${path##*.}, ${path//\//_}, ${path^^}, ${#path}, ${path:0:8}. For the date, strip the prefix up to log. and the suffix .gz: d=${path##*log.}; d=${d%.gz}.
Bonus: Write a function that renames every *.jpeg file in a directory to *.jpg using only parameter expansion and mv --.
A4. A service wrapper with case and exit codes ๐ข
Objective: Use case, [[ ]] and meaningful exit codes to build a small CLI.
Tasks
- Write
svc.sh ACTION SERVICEwhere ACTION isstart,stop,restartorstatus. - Validate the arguments: wrong count or unknown action prints usage to stderr and exits 2.
- Validate the service name against
^[a-z0-9@._-]+$before passing it tosystemctl. statusprintsSERVICE is activeorSERVICE is not active (state)and exits 0 or 3.- Add
-hthat prints usage to stdout and exits 0.
Expected outcome: ./svc.sh status ssh; echo $? prints the state and 0 or 3; ./svc.sh bounce ssh prints usage and exits 2; ./svc.sh status 'ssh; rm -rf /' is rejected.
Hints
case $1 in start|stop|restart) ... ;; status) ... ;; -h) usage; exit 0 ;; *) usage >&2; exit 2 ;; esac. Validate with [[ $2 =~ ^[a-z0-9@._-]+$ ]]. systemctl is-active --quiet "$svc" returns 0 only when active; systemctl is-active "$svc" prints the state.
Bonus: Add --dry-run that prints the systemctl command instead of running it.
A5. Read a CSV without losing data ๐ข
Objective: Write a while read loop that survives spaces, backslashes, a missing final newline and comment lines.
Tasks
- Create
users.csvwith a headerusername,full_name,shell, five rows (one full name containing a backslash, one with leading spaces), a#comment line, a blank line, and no newline after the last row (printfit). - Write
list_users.shthat skips the header, comments and blank lines and printsusername -> full_name (shell)for each row. - Count valid rows in a variable and print the total after the loop.
- Make sure the total is correct (a
cmd | whileversion will print 0: show it, then fix it). - Reject rows whose shell is not listed in
/etc/shells, printing a warning to stderr.
Expected outcome: All five rows print exactly as written (backslash and spaces intact), the last row is not skipped, and the total says 5.
Hints
while IFS=, read -r user name shell || [[ -n $user ]]; do ... done < users.csv. -r keeps backslashes; the || [[ -n $user ]] handles the last line without a newline. A pipeline runs the loop in a subshell, so counters are lost: redirect the file into the loop instead. Skip with [[ -z $user || $user == \#* || $user == username ]] && continue. Check shells with grep -qxF -- "$shell" /etc/shells.
Bonus: Emit each valid row as a JSON object (one per line) with jq -cn --arg, so a name containing quotes or backslashes still produces valid JSON.
A6. A tiny function library ๐ข
Objective: Write reusable functions with local variables, return codes and output, and source them from two scripts.
Tasks
- Create
lib.shwithlog LEVEL MSG(timestamped, to stderr),die MSG [CODE],require_cmd CMD...(exit 127 if missing) andis_port_open HOST PORT(returns 0 or 1, prints nothing). - Use
localfor every variable inside the functions. - Write
check.shthat sourceslib.shrelative to its own location (so it works from any directory) and checks ports 22 and 80 on localhost. - Write
need.shthat callsrequire_cmd jq curl not-a-real-tooland show the exit code. - Demonstrate the difference between a function's return code and its output with
result=$(fn)andif fn; then.
Expected outcome: Both scripts work when run from /tmp and from their own directory; need.sh exits 127 naming the missing tool; logs go to stderr so ./check.sh > /dev/null still shows them.
Hints
Source relative to the script: source "$(dirname -- "${BASH_SOURCE[0]}")/lib.sh". Port check without extra tools: timeout 2 bash -c "</dev/tcp/$1/$2" 2>/dev/null. return sets the status (0 to 255); echo produces output you capture with $(...).
Bonus: Make lib.sh safe to source twice (a guard variable), and make it refuse to be executed directly.
A7. Summarize an access log with a pipeline ๐ข
Objective: Answer real incident questions with grep, awk, cut, sort, uniq and head.
Tasks
- Generate or download an Nginx access log in the combined format with at least 10,000 lines (a small
awkor Bash generator with random IPs, paths and status codes is fine). - Print the top 10 client IPs by request count.
- Print the count of each status code, sorted by code.
- Print the top 5 paths that returned any 5xx status, with counts.
- Print the number of requests per minute for the busiest 5 minutes.
- Put all four reports in one script,
logreport.sh FILE, withset -euo pipefail.
Expected outcome: ./logreport.sh access.log prints four labelled sections and exits 0; it exits non-zero with a clear message when the file does not exist.
Hints
Fields in the combined format: $1 IP, $4 [09/Nov/2026:10:15:02, $7 path, $9 status. Top IPs: awk '{print $1}' f | sort | uniq -c | sort -rn | head. 5xx paths: awk '$9 ~ /^5/ {print $7}'. Per minute: awk '{print substr($4, 2, 17)}'. uniq only merges adjacent lines, so always sort first.
Bonus: Rewrite the whole report as a single awk program that reads the file once, and compare the run time with time.
A8. Safe cleanup with find and xargs ๐ข
Objective: Select files by age, size and name, and delete them safely, including names with spaces and newlines.
Tasks
- Create
/tmp/a8with 50 files: varied ages (touch -d '10 days ago'), some with spaces, one with a newline in its name, and a few larger than 10 MB (fallocate -l 12M). - Write
cleanup.sh DIR DAYSthat lists files older than DAYS days, with their sizes, and the total size that would be freed. - Add
--delete: without it the script only reports (dry run is the default). - Make it NUL-safe end to end (
-print0,xargs -0or-exec ... +). - Refuse to run when DIR is empty,
/, or not under/tmpor/var/tmp.
Expected outcome: The report matches find DIR -type f -mtime +DAYS | wc -l; --delete removes exactly those files including the one with a newline; ./cleanup.sh / 1 --delete is refused.
Hints
find "$dir" -type f -mtime +"$days" -printf '%s\t%p\0' gives size and path, NUL-terminated. Sum with awk -v RS='\0' -F'\t' '{s+=$1} END {print s}'. Delete with find ... -delete or -print0 | xargs -0 -r rm -f --. Guard: [[ $dir == /tmp/* || $dir == /var/tmp/* ]] || die "refusing".
Bonus: Print the freed space in human units with numfmt --to=iec.
A9. A disk alert that really runs from cron ๐ข
Objective: Write a check that works under cron's minimal environment and leaves evidence that it ran.
Tasks
- Write
diskcheck.shthat checks every real filesystem (skiptmpfs,overlay,squashfs) and printsWARNINGat 80% andCRITICALat 90%, for blocks and inodes. - Exit 0, 1 or 2 depending on the worst result.
- Run it with
env -i /path/to/diskcheck.shto simulate cron's empty environment and fix whatever breaks. - Schedule it every 10 minutes in your user crontab, writing to
~/logs/diskcheck.logwith a timestamp on every line. - Fill a filesystem past 80% with
fallocateand confirm the log shows the warning within 10 minutes; then remove the file.
Expected outcome: The crontab line uses absolute paths, the log shows a run every 10 minutes, the warning appears and disappears, and env -i runs cleanly.
Hints
df -P -x tmpfs -x overlay -x squashfs -x devtmpfs and df -Pi ... for inodes; strip % with awk '{sub(/%/, "", $5); print $5, $6}'. Set PATH=/usr/local/bin:/usr/bin:/bin at the top of the script. Crontab: */10 * * * * /home/you/bin/diskcheck.sh >> /home/you/logs/diskcheck.log 2>&1. Timestamp lines inside the script with printf '%(%F %T)T ...' -1.
Bonus: Remember the last state in a file and print only when the state changes.
A10. Generate config files with here-docs ๐ข
Objective: Use quoted and unquoted here-doc delimiters correctly, and render templates without breaking $ that belongs to the target format.
Tasks
- Write
mkvhost.sh DOMAIN PORTthat writes an Nginx server block to./out/DOMAIN.confwith a here-doc, substituting the domain and upstream port. - The block must contain Nginx variables (
$host,$remote_addr,$proxy_add_x_forwarded_for) that must not be expanded by Bash. - Write a second version that keeps the template in
vhost.conf.tmpland renders it withenvsubst, substituting onlyDOMAINandPORT. - Validate inputs: the domain must match a hostname regex and the port must be 1 to 65535.
- Write the output atomically (temp file in the same directory, then
mv).
Expected outcome: Both versions produce identical files; grep -c '\$host' out/*.conf shows the Nginx variables survived; running nginx -t -c against a minimal wrapper config (or in an nginx container) passes.
Hints
In an unquoted here-doc (<<CONF) escape Nginx variables as \$host. With a quoted delimiter (<<'CONF') nothing expands, which is what you want for templates. envsubst '${DOMAIN} ${PORT}' < vhost.conf.tmpl substitutes only the listed variables. Port check: [[ $port =~ ^[0-9]+$ ]] && (( port >= 1 && port <= 65535 )).
Bonus: Use <<- with tab indentation inside a function so the template lines up with the code.
Intermediate
A11. Strict-mode bug hunt ๐ก
Objective: Learn exactly where set -euo pipefail protects you and where it silently does not.
Tasks
- Save this script as
buggy.shand run it. Note what it prints and its exit code.#!/usr/bin/env bash set -euo pipefail count=0 check() { false; echo "check kept going"; } if check; then echo "check passed"; fi get() { local v=$(cat /nonexistent); echo "v=[$v]"; } get errors=$(grep -c ERROR /dev/null) ((count++)) echo "count=$count errors=$errors" - For each line, write down whether strict mode should stop the script there and why it does or does not.
- Fix the script so that every real failure stops it with a message naming the line, and every expected "no result" is handled explicitly.
- Add
set -E,shopt -s inherit_errexitand anERRtrap that prints$LINENO,$BASH_COMMANDand the exit status. - Prove each fix with a one-line command that fails before and passes after.
Expected outcome: A table of five pitfalls (function in if, local x=$(...), grep -c with no match, ((count++)) from 0, command substitution subshells) and a fixed script with zero ShellCheck findings that fails loudly only on real errors.
Hints
set -e is ignored inside anything called from an if condition. local masks the substitution's status (SC2155): declare and assign separately. grep -c prints 0 and exits 1: use || true on that one command. ((count++)) returns 1 when count was 0: use count=$((count + 1)). A trap: trap 'echo "ERR line $LINENO: $BASH_COMMAND ($?)" >&2' ERR.
Bonus: Write the same five pitfalls as five bats tests that each assert the fixed behaviour.
A12. A real CLI with getopts ๐ก
Objective: Build a command-line interface that humans and CI can both rely on.
Tasks
- Write
release.shaccepting-e ENV(required:dev,stagingorprod),-v VERSION(required, semantic version),-n(dry run),-y(skip confirmation) and-h. - Unknown options and missing option arguments must print a specific error plus usage to stderr and exit 2.
- Validate
VERSIONagainst^v?[0-9]+\.[0-9]+\.[0-9]+$andENVagainst an allow-list. - For
prodwithout-y, ask for confirmation only when stdin is a terminal; when it is not (CI), refuse unless-ywas given. - Remaining positional arguments (after
--) are passed through as extra flags to the deploy command, preserved exactly as given.
Expected outcome: ./release.sh -e prod -v 1.4.2 -n -y -- --force "two words" prints the command it would run with the extra arguments intact; ./release.sh -e qa -v 1.4.2 exits 2; echo | ./release.sh -e prod -v 1.4.2 refuses without -y.
Hints
while getopts ':e:v:nyh' opt; do case $opt in ...; :) echo "-$OPTARG needs an argument"; usage; exit 2;; \?) echo "unknown option -$OPTARG"; ...; esac; done; shift $((OPTIND - 1)). The leading : enables silent error handling. Terminal check: [[ -t 0 ]]. Keep extras as extra=("$@") and expand as "${extra[@]}".
Bonus: Support long options (--env, --version, --dry-run) with a manual while (( $# )) loop, and explain why getopt (with a t) is not portable to macOS.
A13. Traps that always clean up ๐ก
Objective: Guarantee cleanup on success, failure, Ctrl+C and SIGTERM, and understand what cannot be trapped.
Tasks
- Write
work.shthat creates a temp directory withmktemp -d, writes a lock file, and runs a 60-second loop that writes progress into the temp directory. - Install traps so the temp directory and lock are removed on normal exit, on any error, on Ctrl+C (exit 130) and on
SIGTERM(exit 143). - Test each case: let it finish, force an error midway, press Ctrl+C, and
kill -TERMit from another terminal. Confirm cleanup each time withls. - Kill it with
kill -9and observe what is left behind. Explain why. - Make the script detect and remove leftovers from a previous
kill -9run on start-up, safely.
Expected outcome: No leftovers after the first four cases, a documented leftover after kill -9, and a start-up routine that cleans it without touching another live run.
Hints
trap cleanup EXIT, trap 'exit 130' INT, trap 'exit 143' TERM: the signal traps call exit, which then runs the EXIT trap once. Use single quotes in trap strings. SIGKILL cannot be trapped. Safe leftover removal: take an flock first, then anything left with your naming pattern belongs to a dead run.
Bonus: Run a child process (sleep 300 &) from the script and make sure Ctrl+C also stops the child (kill -- -$$ or tracking PIDs and wait).
A14. Inventory with indexed and associative arrays ๐ก
Objective: Use arrays to hold structured data and to build commands without string-splitting bugs.
Tasks
- Write an INI-style inventory
hosts.iniwith groups[web],[db],[cache], 2 to 4 hosts each, comments and blank lines. - Parse it in pure Bash into an associative array
group -> "space separated hosts"and an indexed array of all hosts. - Implement
inventory.sh list,inventory.sh hosts GROUP, andinventory.sh count(hosts per group, sorted by group name). - Build an
rsynccommand in an array with conditional options (--dry-runwhenDRY_RUN=1,--excludeentries from a list that contains a pattern with a space) and print it withprintf '%q '. - Reject unknown groups with a helpful message listing the valid ones.
Expected outcome: ./inventory.sh hosts db prints the db hosts one per line; the printed rsync command shows each option as a separate, correctly quoted argument.
Hints
declare -A groups=(). Detect a section with [[ $line =~ ^\[([a-z]+)\]$ ]] && group=${BASH_REMATCH[1]}. Append: groups[$group]+="$host ". Sorted keys: printf '%s\n' "${!groups[@]}" | sort. Options: opts=(-a --delete); [[ ${DRY_RUN:-0} == 1 ]] && opts+=(--dry-run); for e in "${excludes[@]}"; do opts+=(--exclude "$e"); done.
Bonus: Add host variables (web1 ansible_port=2222) stored in a second associative array keyed by host.
A15. JSON and YAML in scripts with jq and yq ๐ก
Objective: Query, transform and edit structured data safely from Bash.
Tasks
- Save the output of
kubectl get pods -A -o json(or any similar JSON; a sample from a kind cluster is fine) topods.json. - With
jq, print a TSV of namespace, pod name, phase and total restart count, sorted by restarts descending. - Print pods whose containers were last terminated with reason
OOMKilled, handling pods with nolastStatewithout errors. - Pass a namespace from a shell variable into the filter with
--arg, never by string interpolation. - With mikefarah
yqv4, changeimage.tagin a Helmvalues.yamlto the value of$TAG, add apodAnnotations.deployedBykey, and show the diff.
Expected outcome: A script k8s-report.sh producing the TSV and the OOMKilled list; values.yaml updated in place with the comments preserved; no null or Cannot iterate over null errors.
Hints
Restarts: [.status.containerStatuses[]?.restartCount] | add // 0. TSV: [.metadata.namespace, .metadata.name, .status.phase, $r] | @tsv. OOMKilled: select(any(.status.containerStatuses[]?; .lastState.terminated.reason? == "OOMKilled")). yq: TAG=1.4.2 yq -i '.image.tag = strenv(TAG) | .podAnnotations.deployedBy = "ci"' values.yaml.
Bonus: Produce the same report as CSV with @csv, and as a Markdown table suitable for a pull request comment.
A16. A parallel fleet runner ๐ก
Objective: Run a command across many hosts with bounded concurrency, per-host logs and a reliable summary.
Tasks
- Write
fleet.sh -f HOSTS_FILE -p PARALLEL -- COMMAND...that runs COMMAND on each host over SSH with-o BatchMode=yes -o ConnectTimeout=5. - Limit concurrency to PARALLEL jobs at a time.
- Save each host's stdout and stderr to
logs/HOST.logand its exit code tologs/HOST.rc. - Print a summary table (host, exit code, duration) sorted with failures first, and exit with the number of failed hosts (capped at 125).
- Make Ctrl+C stop all running SSH sessions and still print the partial summary.
Expected outcome: Against 10 hosts (containers with sshd, or localhost aliases in ~/.ssh/config), the run never exceeds PARALLEL concurrent sessions and the summary matches the .rc files exactly.
Hints
Bound concurrency in Bash: (( $(jobs -rp | wc -l) >= max )) && wait -n before each launch. Or delegate: xargs -a hosts -P "$max" -I{} ./one_host.sh {}. Use ssh -n so SSH does not swallow the loop's stdin. Time each job with SECONDS. For Ctrl+C, trap 'kill $(jobs -p) 2>/dev/null' INT and then print the summary from what exists.
Bonus: Add --canary N: run on the first N hosts, stop if any fails, otherwise continue with the rest.
A17. From cron to a sandboxed systemd timer ๐ก
Objective: Convert a cron job into a service and timer pair, with logging, sandboxing and failure alerts.
Tasks
- Take your
diskcheck.shfrom A9 (or any script) and writediskcheck.service(Type=oneshot, dedicated user,SyslogIdentifier=) anddiskcheck.timer(every 10 minutes, starting 2 minutes after boot). - Add sandboxing:
ProtectSystem=strict,ProtectHome=yes,PrivateTmp=yes,NoNewPrivileges=yes, and only theReadWritePaths=the script needs. - Add
OnFailure=pointing at a template unit[email protected]that writes the failing unit name to the journal (or posts to a webhook). - Remove the crontab entry, enable the timer, and show
systemctl list-timersandjournalctl -u diskcheck. - Run
systemd-analyze security diskcheck.serviceand improve the score with at least two more directives.
Expected outcome: The timer fires every 10 minutes, output is in the journal with your identifier, a forced failure (exit 3) triggers [email protected], and the security score is lower than at the start.
Hints
Timer: OnBootSec=2min, OnUnitActiveSec=10min, [Install] WantedBy=timers.target. Template: ExecStart=/usr/bin/logger -t notify "unit %i failed on %H". After editing units, systemctl daemon-reload. Directives that help the score: ProtectKernelTunables=yes, ProtectControlGroups=yes, RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6, CapabilityBoundingSet=, PrivateDevices=yes.
Bonus: If your monitoring considers WARNING (exit 1) a valid result, use SuccessExitStatus=1 so only real failures trigger OnFailure=.
A18. Log housekeeping that is idempotent ๐ก
Objective: Write a retention script that is safe to run any number of times, concurrently, on a live application directory.
Tasks
- Simulate an app writing
app-YYYY-MM-DD.logdaily: generate 40 days of files and keep a writer appending to today's file. - Write
logkeep.sh DIRthat gzips files older than 1 day (never today's), deletes.gzfiles older than 14 days, and then deletes the oldest.gzfiles until the directory is under a size budget (for example 200 MB). - Make it idempotent: a second immediate run changes nothing and reports
nothing to do. - Make it safe to run concurrently (two runs at once must not double-compress or race) with
flock. - Verify each gzip before deleting the original (
gzip -t), and never leave both a.logand a.log.gzfor the same day.
Expected outcome: After one run: today's log untouched, days 2 to 14 compressed, older ones gone, total size under budget. A second run and two parallel runs both leave the directory unchanged.
Hints
Select by the date in the name, not just mtime: compare ${f//[!0-9]/} with $(date -d '1 day ago' +%Y%m%d). Compress to a temp name and rename: gzip -c "$f" > "$f.gz.tmp" && gzip -t "$f.gz.tmp" && mv "$f.gz.tmp" "$f.gz" && rm -- "$f". Size: du -sb "$dir" | cut -f1. Lock: exec 9>"$dir/.logkeep.lock"; flock -n 9 || exit 0.
Bonus: Write the same policy as a logrotate config and explain in two sentences when you would prefer each.
A19. ShellCheck to zero and a bats suite in CI ๐ก
Objective: Bring three existing scripts to zero ShellCheck findings and cover them with tests that stub external commands.
Tasks
- Pick three of your earlier scripts (for example A4, A9 and A12) and run
shellcheck -xon them. Fix every finding; for any you suppress, write the reason in the same comment. - Add a
.shellcheckrcwithsource-path=SCRIPTDIRandexternal-sources=true. - Write at least 10 bats tests across the three scripts covering success, usage errors, and failure paths.
- Stub
systemctl,dfandsshwith scripts intest/stubs/placed first onPATH, recording their arguments to a log you assert on. - Add a
Makefilewithlintandtesttargets and a GitHub Actions workflow runningmake lint teston every pull request.
Expected outcome: make lint test is green locally and in CI; deliberately introducing an unquoted variable makes CI fail on ShellCheck; deliberately breaking an exit code makes a bats test fail.
Hints
In bats, run cmd captures $status and $output; each test gets a fresh $BATS_TEST_TMPDIR. Put export PATH="$BATS_TEST_DIRNAME/stubs:$PATH" in setup(). Remember that a bare ! cmd in the middle of a test does not fail it; assert counts or use run !. Install in CI with sudo apt-get install -y shellcheck bats.
Bonus: Add a pre-commit hook (.git/hooks/pre-commit or the pre-commit framework) that runs ShellCheck on staged *.sh files only.
A20. Release directories, atomic switch and automatic rollback ๐ก
Objective: Write a deploy script whose switch-over is atomic and whose rollback is automatic when a health check fails.
Tasks
- Create
/srv/demo/releases/and acurrentsymlink. Each release is a directory named by UTC timestamp containing a tiny app (for examplepython3 -m http.serverserving aversion.txt). - Write
deploy.sh ARTIFACT.tar.gzthat unpacks into a new release directory, runs a pre-flight check, switchescurrentatomically, and restarts the service. - Poll a health URL for up to 30 seconds after the switch; if it does not report the new version, switch back to the previous release and exit non-zero.
- Keep the newest 5 releases, never deleting the one
currentpoints to. - Write
rollback.shthat switches to the previous release on demand.
Expected outcome: A good artifact deploys with zero failed requests (run a curl loop during the deploy); a bad artifact (health never passes) is rolled back automatically and the script exits non-zero.
Hints
Atomic switch: ln -sfn "$new" /srv/demo/current.tmp && mv -T /srv/demo/current.tmp /srv/demo/current (rename is atomic; ln -sfn alone is not). Previous release: readlink -f /srv/demo/current before switching. Keep 5: ls -1d /srv/demo/releases/*/ | sort | head -n -5, skipping the current target.
Bonus: Add flock so two deploys cannot overlap, and log every deploy (who, when, which release, result) to a JSON Lines audit file.
Advanced
A21. Expansion-order puzzles and production-grade tracing ๐ด
Objective: Predict Bash's behaviour from first principles and debug scripts with traces that are useful and safe.
Tasks
- Predict, then verify, the output of each line, and explain each result by naming the expansion step responsible:
x='*'; echo $x "$x" a='1 2'; set -- $a; echo $# echo {1..3}{a,b} v='$HOME'; echo $v "$v" $(echo $v) n=5; echo {1..$n} IFS=:; p='a:b::c'; set -- $p; echo $# f() { echo "$#"; }; arr=('a b' c); f ${arr[@]}; f "${arr[@]}"; f "${arr[*]}" - Write a 100-line script with a subtle bug (your choice) and find it using
bash -xwith a customPS4that shows file, line and function. - Send the trace to a separate file with
BASH_XTRACEFDso stdout and stderr stay clean. - Make sure a secret (
API_TOKEN) never appears in the trace, even with tracing on. - Enable tracing on demand with
DEBUG=1without editing the script.
Expected outcome: A written explanation for all seven puzzles, a trace file where every line reads like + deploy.sh:42:upload(): curl ..., and a grep showing the token is absent from the trace.
Hints
Brace expansion happens first, before parameter expansion, which is why {1..$n} does not work. Word splitting and globbing apply only to unquoted expansion results. Two adjacent non-whitespace IFS characters (like ::) delimit an empty field. PS4='+ ${BASH_SOURCE##*/}:${LINENO}:${FUNCNAME[0]:-main}(): '. exec 7>>trace.log; BASH_XTRACEFD=7; set -x. Wrap secret use in { set +x; } 2>/dev/null; ...; set -x, and pass secrets via stdin or files rather than arguments.
Bonus: Measure where a slow script spends its time by adding $EPOCHREALTIME to PS4 and sorting the gaps between lines.
A22. Port a Bash script to POSIX sh ๐ด
Objective: Know exactly which features are Bash-only by porting a real script to dash and BusyBox ash.
Tasks
- Take a 100+ line Bash script that uses arrays,
[[ ]],${var//a/b},$(< file),<(...),local,read -aandprintf -v(your A14 or A18 script works). - Port it to POSIX
shwith the shebang#!/bin/sh, preserving behaviour. - Check it with
shellcheck -s shandcheckbashisms(fromdevscripts), reaching zero findings. - Test it under
dashon Ubuntu and under BusyBox inalpine:3.20, with the same bats tests you run against the Bash version. - Write a table of every construct you replaced and its POSIX equivalent.
Expected outcome: The same test suite passes on Bash, dash and BusyBox ash; your table lists at least eight replacements.
Hints
Arrays become set -- ... positional parameters or newline-separated strings. [[ $a == b* ]] becomes case $a in b*) ...;; esac. ${var//a/b} becomes printf '%s' "$var" | sed 's/a/b/g'. Process substitution becomes a temp file or a FIFO from mkfifo. local is not POSIX, though dash and BusyBox support it. Run tests per shell with bats by invoking the script as "$SHELL_UNDER_TEST" script.sh.
Bonus: Measure start-up and run time of the Bash and dash versions with hyperfine or a time loop, and explain the difference.
A23. Security review and hardening of a hostile-input script ๐ด
Objective: Find and fix injection, unsafe temp files, secret leakage and privilege problems in a script that processes untrusted input.
Tasks
- Save this script, then list every vulnerability you can find, with a one-line exploit for each:
#!/bin/bash # process uploaded file: ./process.sh "<filename>" "<user>" TOKEN=$(cat /etc/app/token) cd /srv/uploads eval "owner=$2" tmp=/tmp/process.$$ cp $1 $tmp curl -s -H "Authorization: Bearer $TOKEN" "https://api.example.com/scan?file=$1" > /tmp/result if grep -q clean /tmp/result; then mv $tmp /srv/clean/$1; fi ssh backup "gzip /archive/$1" rm -rf /srv/uploads/$1 - Rewrite it with strict mode, input validation (allow-list regex), no
eval,mktemp,--before filenames, the token kept out ofargvand out of traces, URL-encoded query parameters, and safe remote quoting. - Make it refuse to run as root and run the privileged step (moving into
/srv/clean) through a narrowly scopedsudorule. - Write bats tests that feed hostile filenames (
../../etc/passwd,$(id),a;rm -rf ~,-rf, a name with a newline) and assert nothing outside the upload directory is touched. - Get the rewrite to zero ShellCheck findings.
Expected outcome: At least eight vulnerabilities documented with exploits; a hardened script that passes all hostile-input tests; a sudoers rule that allows exactly one command.
Hints
Issues include: eval on user input, unquoted $1 (splitting, globbing, option injection), predictable temp files (symlink attacks), a shared /tmp/result (race), the token visible in ps via -H, path traversal into /srv/clean, the remote shell re-parsing $1, and an unchecked cd before rm -rf. Use curl --config <(printf 'header = "Authorization: Bearer %s"\n' "$TOKEN"), jq -rn --arg v "$name" '$v|@uri' for URL encoding, and printf %q for remote arguments.
Bonus: Run the hardened script under systemd-run --pty -p ProtectSystem=strict -p ReadWritePaths=/srv/uploads ... and show that a remaining bug still cannot write outside its paths.
A24. A resumable bulk job that survives kill -9 ๐ด
Objective: Process thousands of items reliably: checkpointing, idempotency, retries with backoff and jitter, rate limiting and atomic outputs.
Tasks
- Generate 5,000 item IDs. Write
bulk.shthat "processes" each item by calling a fake API script that randomly fails (20% of calls exit 1, 2% hang for 60 seconds) and writes a result file per item. - Retry failed calls up to 5 times with exponential backoff and random jitter; time out hung calls after 10 seconds.
- Record completed IDs in a checkpoint file so a restart skips them, and write each result atomically so a killed run never leaves a partial result file.
- Limit throughput to N calls per second and M in parallel.
- Kill the job with
kill -9at random points five times, restarting each time, and prove that every item ends up processed exactly once with a valid result.
Expected outcome: After the five kills and a final run, exactly 5,000 valid result files exist, the checkpoint has 5,000 unique IDs, and a summary reports retries, timeouts and permanent failures.
Hints
Backoff: sleep $(( (2 ** attempt) + RANDOM % 3 )). Timeout: timeout --kill-after=2 10 ./fake_api.sh "$id". Atomic result: write to results/$id.json.tmp.$$, validate (jq -e .), then mv. Checkpoint appends with flock around echo "$id" >> done.txt when running in parallel; on start, load it into an associative array. Order of operations matters: write the result, then checkpoint, and make processing idempotent so a crash between the two is harmless.
Bonus: Expose progress (done, failed, rate, ETA) to a Prometheus textfile collector file, rewritten atomically every 10 seconds.
A25. When Bash has been outgrown: measure, then decide ๐ด
Objective: Make an evidence-based decision about rewriting a script, using profiling rather than opinion.
Tasks
- Generate a 2-million-line access log. Write a "naive" Bash report that loops over lines with
while readand callscut,dateandgrepper line to compute requests per minute and the top 10 IPs with 5xx responses. - Measure it with
timeandstrace -c -f(countcloneandexecve). Stop it after 5 minutes if needed and extrapolate. - Write a builtins-only Bash version (parameter expansion,
[[ =~ ]],printf '%(...)T', associative arrays) and measure again. - Write a single-
awkversion and a short Python version, and measure both. - Write a one-page decision memo: run times, fork counts, readability, testability, team skills and deployment constraints, ending with a recommendation and the criteria that would change it.
Expected outcome: A results table showing orders-of-magnitude differences between the naive loop and the awk/Python versions, and a memo that a senior engineer could approve or challenge on specific points.
Hints
The naive version forks several processes per line, so its cost is roughly lines times fork cost. strace -c -f -o stats.txt ./report.sh summarises syscalls. In awk, group with associative arrays: { split($4, t, ":"); minute[t[2] ":" t[3]]++ } $9 ~ /^5/ { ip[$1]++ }. Typical rewrite triggers: complex data structures, JSON-heavy logic, API pagination and retries, concurrency, or more than a few hundred lines.
Bonus: Add the fastest version to a CI job with a performance budget that fails if run time on a fixed sample regresses by more than 20%.