Exit Status and $?
In this page:
Reading $? After a Command
After any command finishes, Bash stores its exit status in the special variable $?. You can inspect it immediately afterward to decide what your script should do next, but it gets overwritten the instant another command runs.
Example: Reading $? After a Command
#!/bin/bash
echo "hello"
echo "Exit status of echo: $?"
grep "zzz" <<< "no match here"
echo "Exit status of grep (1 = no match found): $?"
Login to try C/C++/Java/PHP code in the editor
Success vs Failure Codes
Exit status 0 conventionally means the command succeeded, while any value from 1 to 255 signals failure, though the exact meaning of each non-zero number is defined by that specific command. Many commands only ever return 0 or 1, but some use different numbers to distinguish different kinds of errors.
Example: Success vs Failure Codes
#!/bin/bash
true
echo "true's exit status: $?"
false
echo "false's exit status: $?"
Login to try C/C++/Java/PHP code in the editor
&& and || Based on Exit Status
&& chains a second command to run only if the first one exited with status 0 (success). || does the opposite, running the second command only if the first exited non-zero (failure), which is a common shorthand for simple conditional logic.
Example: && and || Based on Exit Status
#!/bin/bash
mkdir -p /tmp/demo_$$ && echo "Directory created successfully"
grep "missing" <<< "nothing to find" || echo "Pattern not found, handled gracefully"
Login to try C/C++/Java/PHP code in the editor
Setting Your Script's Own Exit Status
A script or function can explicitly set the exit status callers will see using the exit builtin followed by a number. If exit is called with no argument, the script exits with the status of the last command it ran.
Example: Setting Your Script's Own Exit Status
#!/bin/bash
check_value=5
if [ "$check_value" -gt 0 ]; then
echo "Value is positive"
exit 0
fi
echo "This line would only run for non-positive values"
exit 1
Login to try C/C++/Java/PHP code in the editor
- Assuming a non-zero exit status always means a crash; it usually just means the command's own definition of failure, e.g.
grepreturns 1 simply when it finds no matches, which is not an error condition. - Checking
$?after running an unrelated command in between, likeechofor debugging;$?always reflects the *most recently* completed command, so any command run after the one you care about overwrites it. - Forgetting that exit status is capped at 0-255; larger values wrap around due to how the value is stored, which can produce surprising results.
$?holds the exit status of the most recently executed command.- By convention, exit status
0means success, and any non-zero value (1-255) means failure or a specific error condition. &&runs the next command only if the previous one exited 0;||runs the next command only if the previous one exited non-zero.- A script's own exit status can be set explicitly with the
exitbuiltin, e.g.exit 1.
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: