Bash Scripting for Engineers›Modules · Cheat sheet & self-check

Modules · wrap-up

Cheat sheet & self-check

25 questions across 8 lessons. Each answer links back to the lesson it came from.

Pick an answer to see if you got it, and why.

  1. Q1. What's wrong with 'name = Asha' in Bash?

    Show answer

    B. Assignments must be written name=value with no spaces. It's the most common first Bash error.

    From lesson 01 · Scripts, variables & quoting
  2. Q2. file='my report.txt'; rm $file — what happens?

    Show answer

    B. Unquoted variables are split on spaces. Always quote: rm "$file".

    From lesson 01 · Scripts, variables & quoting
  3. Q3. What does an exit status of 0 mean?

    Show answer

    B. By convention 0 = success and anything else (1–255) = some kind of failure. if, &&, || and set -e all rely on it.

    From lesson 01 · Scripts, variables & quoting
  4. Q4. Which prints the literal text $HOME?

    Show answer

    B. Single quotes stop all expansion. Double quotes still expand variables.

    From lesson 01 · Scripts, variables & quoting
  5. Q5. Which test checks that a directory exists?

    Show answer

    B. -d is true for directories, -f for regular files, -e for anything that exists.

    From lesson 02 · Conditions & loops
  6. Q6. Why use 'while IFS= read -r line' instead of 'for line in $(cat file)'?

    Show answer

    B. for over $(cat) iterates words, not lines. IFS= keeps leading spaces, and -r keeps backslashes.

    From lesson 02 · Conditions & loops
  7. Q7. How do you compare numbers in Bash?

    Show answer

    B. Inside [[ ]], > compares strings alphabetically. Use (( )) for arithmetic, or -gt/-lt/-eq in [[ ]].

    From lesson 02 · Conditions & loops
  8. Q8. Why write "$@" instead of $* when passing arguments on?

    Show answer

    B. Unquoted $* and $@ re-split arguments. "$@" preserves exactly what the caller passed.

    From lesson 03 · Functions & arguments
  9. Q9. What does 'local' do inside a function?

    Show answer

    B. Without local, every assignment in a function changes the global variable. That's a common source of subtle bugs.

    From lesson 03 · Functions & arguments
  10. Q10. Where should a script print error messages?

    Show answer

    B. stdout is for the script's output (possibly piped into another tool); stderr is for diagnostics.

    From lesson 03 · Functions & arguments
  11. Q11. Which command changes 'debug' to 'info' in config.ini in place, keeping a backup?

    Show answer

    B. Without -i, sed only prints the result. -i.bak edits the file and saves the original as config.ini.bak.

    From lesson 04 · Text processing
  12. Q12. What does awk -F: '{print $1}' /etc/passwd print?

    Show answer

    B. -F: sets the field separator to ':'. $1 is the first field.

    From lesson 04 · Text processing
  13. Q13. kubectl get pods -o json | jq -r '.items[].metadata.name' prints…

    Show answer

    B. .items[] iterates the list, .metadata.name selects the field, and -r prints raw strings without JSON quotes.

    From lesson 04 · Text processing
  14. Q14. What does 'set -o pipefail' change?

    Show answer

    B. Without pipefail, 'curl … | tar x' reports tar's status only, hiding a failed download.

    From lesson 05 · Safe scripts
  15. Q15. Why is 'rm -rf "$DIR"/' dangerous, and what's the fix?

    Show answer

    B. ${DIR:?} makes Bash stop with an error when DIR is unset or empty, before rm ever runs.

    From lesson 05 · Safe scripts
  16. Q16. What makes a script idempotent?

    Show answer

    B. Idempotent steps (check before acting, mkdir -p, 'ensure' rather than 'create') make re-runs after a partial failure safe.

    From lesson 05 · Safe scripts
  17. Q17. A cron job sometimes runs longer than its interval, and two copies corrupt data. Simplest fix?

    Show answer

    B. flock on a lock file serialises runs reliably, and the lock is released automatically when the process exits.

    From lesson 05 · Safe scripts
  18. Q18. A script works in your terminal but fails from cron with 'command not found'. Most likely cause?

    Show answer

    B. cron doesn't load your .bashrc. PATH is minimal, and other variables are missing. Set what you need explicitly.

    From lesson 06 · Scheduling & automation
  19. Q19. What does the cron line '*/10 9-17 * * 1-5' mean?

    Show answer

    B. Fields are minute, hour, day-of-month, month, day-of-week. */10 = every 10 minutes; 9-17 = hours 9 to 17; 1-5 = Mon–Fri.

    From lesson 06 · Scheduling & automation
  20. Q20. What does Persistent=true do in a systemd timer?

    Show answer

    B. Persistent timers catch up on missed runs, which is useful for laptops, VMs and servers that reboot during the window.

    From lesson 06 · Scheduling & automation
  21. Q21. shellcheck warns SC2086 'Double quote to prevent globbing and word splitting'. What should you do?

    Show answer

    B. SC2086 is the most common real bug in Bash scripts. Quoting fixes it; intentional exceptions get a '# shellcheck disable=SC2086' with a reason.

    From lesson 07 · Testing & linting
  22. Q22. How do you make functions in a script testable with bats without running the whole script?

    Show answer

    B. A guard like [[ "${BASH_SOURCE[0]}" == "$0" ]] && main "$@" lets tests 'source' the file to load functions without side effects.

    From lesson 07 · Testing & linting
  23. Q23. Why run shellcheck and tests in CI?

    Show answer

    B. Automated checks on every pull request catch regressions consistently, without relying on reviewers noticing quoting bugs.

    From lesson 07 · Testing & linting
  24. Q24. Why does the script exit 3 when curl is missing, instead of 1?

    Show answer

    B. A missing tool is a problem with the monitoring, not with the service. Different codes lead to different alerts and fixes.

    From lesson 08 · Capstone: a real ops script
  25. Q25. Why log in 'key=value' format?

    Show answer

    B. Structured logs turn into dashboards and alerts with no extra parsing work.

    From lesson 08 · Capstone: a real ops script