Interview Scripting (Bash, Groovy)

Why does set -e not always behave as expected (e.g., inside pipelines or conditionals)? [Basic]

Answer

set -e has exceptions: it does not always exit inside condition tests, parts of && or || chains, some subshells, or non-final commands in a pipeline unless pipefail is enabled. I treat it as a safety net, not as complete error handling.

Technical explanation

Commands used in if, while, until, or after ! are expected to sometimes return nonzero, so set -e does not exit there.

Without pipefail, false | true returns success because the pipeline status is the last command's status.

For critical operations, handle failures explicitly with if, ||, or a die function.

Hands-on example

set -e

false | true # script may continue without pipefail

set -o pipefail

if grep -q "needle" file.txt; then

echo "found"

else

echo "not found is expected, not fatal"

fi

cp source target || { echo "copy failed" >&2; exit 1; }

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