Intermediate LevelLinux Commands & SysAdmin
Why should every production Bash script start with set -euo pipefail?
❓ Interview Prompt: Explain production Bash safety flags: set -e (errexit), set -u (nounset), set -o pipefail, and set -x (xtrace debugging).
By default, Bash continues executing subsequent script lines even if a preceding command fails! Adding 'set -euo pipefail' forces strict safety rules:
•
'set -e' (errexit): Exits script immediately if any command returns a non-zero error exit status.
•
'set -u' (nounset): Exits script if an uninitialized variable is referenced (prevents catastrophic 'rm -rf /$UNSET_VAR' bugs!).
•
'set -o pipefail': Ensures pipeline failure returns the exit status of the first failing command in a pipe.
Code Implementation Example
bash
#!/bin/bash
# Enforce strict production safety options
set -euo pipefail
DIR="/var/backups/app"
# Script will abort immediately if DIR is un-set or mkdir fails!
mkdir -p "$DIR"
cd "$DIR"💡Enforcing production script safety using set -euo pipefail.
🎯 Interview Key Takeaway
Start every production Bash script with set -euo pipefail to catch unhandled errors and undefined variables instantly.
#set -euo pipefail#Bash Safety#Scripting#Intermediate
Back to all Linux Commands & SysAdmin Questions→