How do you capture both stdout and the exit code of a command reliably?
⚡
Quick Answer
Capture output with $(...) and check $? immediately, or use PIPESTATUS for pipelines.
Detailed Answer
out=$(cmd) captures stdout; the exit code is in $? on the next line (check it before running anything else). In a pipeline, $? is only the last command's status — use ${PIPESTATUS[0]} to inspect an earlier stage (this is why pipefail matters).
💡
Interview Tip
Mention PIPESTATUS for pipelines — a common gotcha.
bashexit-codepipestatus