That definition you gave, i.e. the exit status of the most recently run process, is exactly what is is; but if you don't get how processes and commands in general work on the command line, I can understand how that might be a bit confusing.
In general, any command you run on the command line will have an exit code. In something like C/C++, you might have seen it as the return 0;
appended at the end of every main()
routine, and in shell scripts you might have seen it as exit 0
. These return codes are the primary way to signal to the external environment (in this case the terminal) that things either ended well, or they didn't.
This is where $?
comes into play. The convention on Unix systems is to have a process return exit code 0
if everything went fine, and return a non-zero value if it didn't. So, let's say I wrote a simple main routine like so:
int main(int argc, char* argv[]) {
if (argv[1] == "true") { // not quite right, but fine for examples
return 0;
}
else if (argv[1] == "false") {
return 1;
}
}
If I run this in the commnand line as ./a.out true
, then this program will return 0 and the $?
variable will be set to 0 as well. If I type ./a.out false
, however, the program will return 1 and $?
will also be set to 1, which indicates that something went wrong.
This seems pretty redundant if you're just toying around in the command line, running commands and then echo $?
to see the result, but this really becomes useful in Bash scripting, where you might want to see what the command you just ran returns. For example, the diff
command returns 0 if the two files you specify don't differ, and 1 if they do. This gives a neat way to introduce some control flow into your program, but you have to be careful, because $?
is set after every command you run.
I hope this helped!