Lessons · Terminal · $? (exit status)
Every command reports success or failure
After any command, $? holds its exit status: 0 means success, anything else means failure. &&, || and scripts all read it.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
This one number is how automation knows whether to carry on. A deploy script that ignores it ships broken builds.
How to think about it
When a chain does something odd, echo $? right after the suspect command and read the number.
Worked example
trueA command that always succeeds.
echo $?0.
falseA command that always fails.
echo $?1.
grep zzz /dev/nullgrep finds nothing.
echo $?1: no match counts as failure for grep.
Your turn
Print the status of the last command.
echo
Try it at a real prompt
The trap
Reading $? two commands later. It is overwritten by every command, including the echo you used to look at it.
Practise $? (exit status) on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.