I am running a program and want to see what its return code is (since it returns different codes based on different errors).
I know in Bash I can do this by running
echo $?
What do I do when using cmd.exe on Windows?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The "exit code" is stored in a shell variable named
errorlevel.The
errorlevelis set at the end of a console application. Windows applications behave a little differently; see @gary‘s answer below.Use the
ifcommand keyworderrorlevelfor comparison:Which will execute statements when the errorlevel is greater than or equal to n. Execute
if /?for details.A shell variable named
errorlevelcontains the value as a string and can be dereferenced by wrapping with %’s.Example script:
Warning: An environment variable named errorlevel, if it exists, will override the shell variable named errorlevel.
if errorleveltests are not affected.