Interview Scripting (Bash, Groovy)

Why is set -euo pipefail considered a safe default for scripts? [Basic]

Answer

set -euo pipefail is considered a safer default because it stops many scripts from continuing with failed commands, unset variables, or hidden pipeline failures. It makes failures more visible and reduces silent data corruption.

Technical explanation

It is useful for deployment, backup, and maintenance scripts where continuing after an error can be dangerous.

It should be paired with intentional exception handling for commands whose failure is expected.

It makes scripts stricter, so optional variables should use defaults like ${var:-default} and required variables should use ${var:?message}.

Hands-on example

#!/usr/bin/env bash

set -euo pipefail

backup_dir=${BACKUP_DIR:-/var/backups/app}

mkdir -p "$backup_dir"

pg_dump app > "$backup_dir/app.sql"

gzip -f "$backup_dir/app.sql

Preparing for an interview?

Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.

More Scripting (Bash, Groovy) interview questions

← All Scripting (Bash, Groovy) questions