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 purpose of the shebang line, and what does #!/bin/bash do? [Basic]
- What is the difference between sh and bash? [Basic]
- How do you make a script executable and run it? [Basic]
- What is the difference between running a script with./script.sh, bash script.sh, and source script.sh? [Basic]
- What does sourcing a script do differently from executing it? [Basic]
- How do you declare a variable in Bash, and why are spaces around = not allowed? [Basic]
- What is the difference between $var and ${var}? [Basic]
- What is the difference between single quotes and double quotes in Bash? [Basic]