3

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

1 Answers1

6

%ERRORLEVEL% is the equivalent in Windows .

BenMorel
  • 4,507
  • 10
  • 57
  • 85
Zypher
  • 37,405
  • 5
  • 53
  • 95
  • 1
    Keep in mind though, that this can fail if there is an actual environment variable named `%ERRORLEVEL%`. See also http://blogs.msdn.com/oldnewthing/archive/2008/09/26/8965755.aspx. So for added robustness you may want to delete the variable before accessing the pseudo-variable. – Joey Jan 15 '10 at 06:28