Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

echo $?  # prints exit code of `bar`

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.)

share|improve this question
up vote 6 down vote accepted

%ERRORLEVEL% is the equivalent in Windows .

share|improve this answer
Keep in mind though, that this can fail if there is an actual environment variable named %ERRORLEVEL%. See also So for added robustness you may want to delete the variable before accessing the pseudo-variable. – Joey Jan 15 '10 at 6:28

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.