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
- What is the difference between [ ], [[ ]], and (( )) in Bash? [Basic]
- What do set -e, set -u, set -o pipefail, and set -x do? [Basic]
- What does an exit code of 0 versus non-zero mean? [Basic]
- What are the common file test operators (-f, -d, -e, -r, -w, -x)? [Basic]
- What is $? used for, and how do you check the exit status of a command? [Basic]
- How do you write a for loop over a list and a C-style for loop? [Basic]
- What is command substitution, and what is the difference between $(...) and backticks? [Basic]
- What is the difference between single quotes and double quotes in Bash? [Basic]