Quiz: Bash / Shell Scripting¶
4 questions
L0 (2 questions)¶
1. What does set -euo pipefail do in a bash script?
Show answer
-e exits on error, -u treats unset variables as errors, -o pipefail makes pipe return the exit code of the first failed command.2. What does 'set -euo pipefail' do and why should every production script start with it?
Show answer
-e: exit immediately on any command failure. -u: treat unset variables as errors (catches typos). -o pipefail: a pipeline fails if any command in it fails (not just the last). Without these, scripts silently continue after errors, producing corrupt state.L1 (1 questions)¶
1. What is a bash trap and how do you use it for cleanup?
Show answer
trap 'cleanup_function' EXIT runs cleanup_function whenever the script exits (normal, error, or signal). Common pattern: trap 'rm -f "$tmpfile"' EXIT. You can trap specific signals: trap 'echo interrupted' INT TERM. Traps execute in LIFO order if multiple are set for the same signal.L2 (1 questions)¶
1. What is process substitution in bash and when is it useful?