If an external command (i.e., a program) has an exit code, its value is stored in the ? variable when the program terminates. Additionally. some internal commands, e.g., DIR - to emulate Microsoft's CMD - also set this variable to the same value they set the variable _?, an action which destroys the code from the last external command.

 

To insure that you use the exit code from the external command you want to check, not that of a subsequent internal or external command, it is best to save the value of ? in another variable immediately on completion of the external command of interest, and use that variable instead. We also strongly recommend that for internal commands you query the _? variable instead.

 

Not all programs return an exit code. If a program does not explicitly return an exit code, the value of %? is undefined.

 

Alternate name: ERRORLEVEL.

 

See also: _?