windowsbatch-fileerrorlevel

Windows batch exit option b with or without errorlevel


I'm newbie in using batch on Windows and have a question about the use of errorlevel.

I referenced TechNet(Exit) and many examples on google.

Most of them used /b with %errorlevel% like this

if errorlevel 1 exit /b %errorlevel%

I wonder the difference between

if errorlevel 1 exit /b

and

if errorlevel 1 exit /b %errorlevel%

I think there are no difference because %errorlevel% is not changed. Am I wrong?


Solution

  • TL;DR

    Most of the time there should be no difference, but technically exit /b %errorlevel% is strictly worse than exit /b if what you want is to exit without changing the error level.

    Analysis

    EXIT /B without the optional errorlevel parameter does not change the error level, so as a standalone command it is exactly equivalent to EXIT /B %errorlevel% provided that %errorlevel% resolves to the current error level.

    But there are cases where it might not do so: