The following Windows batch file called "foo.bat" echos "quitting" and sets the return code to 1 as I expect:

if "1"=="1" (
if "1"=="1" (
echo quitting
exit /B 1
)
)

But, to my surprise, the return code of this batch file is 0:

if "1"=="1" (
if "1"=="1" (
echo quitting
exit /B 1
)
echo anything
)

I determine the batch file's return code like so in the Windows command prompt:

> cmd.exe /c foo.bat
quitting
> echo %ERRORLEVEL%
0

I've verified that ERRORLEVEL is not already set in my environment, running set ERRORLEVEL prints "Environment variable ERRORLEVEL not defined" as expected.

Everything else about the second file works as expected. It does echo "quitting" and does not echo "anything". It appears that adding the echo anything line to the script means that the line exit /B 1 still exits but does not set the return code.

I cannot reproduce that, neither on Windows 7 nor on Windows 10. Have you tested exactly the code you posted?
– aschipflJul 17 '17 at 19:41

How are you testing the return code? Just echo %errorlevel%? You didn't set the errorlevel variable manually, did you?
– SomethingDarkJul 17 '17 at 22:23

I also cannot reproduce your result. Please show the batch script that calls "foo.bat" and tests the returned ERRORLEVEL. If the test was run from the command line, then show the exact sequence of commands that demonstrate the errant result. Also verify that you don't have a user defined ERRORLEVEL environment variable defined that overrides the dynamic value (use set errorlevel - you should get Environment variable errorlevel not defined message)
– dbenhamJul 18 '17 at 3:00

Thanks, I've updated my answer. I've shown the commands I use to test the returned ERRORLEVEL, and I've checked that ERRORLEVEL is not user-defined.
– A. Jesse Jiryu DavisJul 18 '17 at 3:11