In a Bash script, how do you check whether a command succeeded, and how would you make the script stop on the first error?

technical-conceptual · Junior level · software-engineering

What the interviewer is really asking

Assesses whether the candidate writes defensive scripts that fail loudly rather than silently continuing past errors.

What to say

What to avoid

Example answers

Strong: Each command sets $?: zero means success, anything non-zero means failure, and if/then tests that exit code directly so I can write if curl ...; then. To make a whole script bail on the first error I put set -euo pipefail at the top: -e stops on any failing command, pipefail surfaces failures mid-pipeline instead of trusting the last command, and -u catches typos in variable names. When I expect a command might fail, I handle it explicitly so it doesn't trip the abort.

Weak: You can usually tell a command worked by looking at whether it printed an error message. If it printed something red or said error, it failed. Bash scripts stop by themselves when there's a problem, so I don't really do anything special, I just run the commands one after another.

Want questions matched to your role? Paste a job title, job description, or CV and get a personalized set, or go Pro to unlock the full bank.