Check last error code across OS

In general, programs don’t usually print to console the integer return code from the main procedure. The program may well print some message indicating success or failure, but maybe not. When calling executables from a compiled or scripted language such as Fortran, C, Python or Matlab, it’s often vital to know the value of the integer return code as a signal that the program thought it was successful or not. Further, some program crashes do not emit any console text, and could make the user think the program was successful.

To help eliminate doubt, I often issue a command to print the last error code to console when I’m working with command line program. The method to print this integer code depends on the shell. From Terminal / Command Prompt, the return code from the last command is printed by:

  • Mac / Linux: echo $?
  • Windows command prompt: echo %errorlevel%
  • PowerShell: echo $lastexitcode