Windows – Get error code of last shell command

windowswindows-command-prompt

Using bash, the exit code of the last thing that executed is stored in the $? shell variable:

foo
bar
echo $?  # prints exit code of `bar`

baz
echo $?  # prints exit code of `baz`

Is there an equivalent way to get this value if I'm running a script in cmd under Windows? (Note: resorting to things like running a Python script which then invokes its own library functions is not what I'm after here.)

Best Answer

%ERRORLEVEL% is the equivalent in Windows .