The Difference Between EXIT and EXIT /B in a Batch Script
In a batch script, the EXIT command is used to terminate the script's execution. However, it comes in two primary forms (EXIT and EXIT /B) that have a critically important difference in behavior, especially when one script is calling another. Understanding this distinction is essential for writing clean, modular scripts and for controlling how error codes are passed between them.
This guide will clearly explain the difference between the two commands, demonstrate the behavior of each with clear examples, and establish the modern best practice of using EXIT /B for almost all scripting needs.
The Core Command: EXIT
The EXIT command tells the command processor (cmd.exe) to stop processing and exit. Where it "exits" to depends entirely on the /B switch.
EXIT: Terminating the Entire cmd.exe Session
When you use the EXIT command by itself (without the /B switch), it has a very drastic and powerful effect: it terminates the entire cmd.exe process that is running the script.
- If you double-clicked the script: The command prompt window that appeared will immediately close. This is often the desired behavior.
- If you ran the script from an existing, interactive command prompt: The command prompt window you were working in will be terminated and closed. Any unsaved state (like the current directory or temporary variables) will be lost. This is often a very undesirable and surprising side effect.
EXIT /B: Exiting Just the Batch Script
The /B switch completely changes the command's behavior. When you use EXIT /B, it exits the current batch script only.
- The execution of the current script stops immediately.
- Control is returned to whatever called the script.
- If it was another batch script (using
CALL), the calling script will resume on the next line. - If it was an interactive command prompt, you are simply returned to the prompt. The window does not close.
- If it was another batch script (using
The /B switch effectively turns EXIT into a "return" statement for your script or subroutine.
The Critical Difference Demonstrated
Let's use two scripts to show the difference.
The "Child" Script (ChildScript.bat)
This script will take one argument: either PlainExit or B_Exit.
@ECHO OFF
ECHO [ChildScript] Starting up.
ECHO [ChildScript] The argument I received was: %1
IF /I "%1"=="PlainExit" (
ECHO [ChildScript] Now exiting with a plain EXIT command...
EXIT
)
IF /I "%1"=="B_Exit" (
ECHO [ChildScript] Now exiting with EXIT /B...
EXIT /B
)
The "Parent" Script (ParentScript.bat)
This script will CALL the child script.
@ECHO OFF
ECHO --- [ParentScript] Starting ---
ECHO.
ECHO --- [ParentScript] Calling ChildScript with 'PlainExit' ---
CALL ChildScript.bat PlainExit
ECHO --- [ParentScript] This line will NEVER be reached. ---
Run ParentScript.bat from a command prompt and obtain this output:
--- [ParentScript] Starting ---
--- [ParentScript] Calling ChildScript with 'PlainExit' ---
[ChildScript] Starting up.
[ChildScript] The argument I received was: PlainExit
[ChildScript] Now exiting with a plain EXIT command...
...and at this point, the entire command prompt window closes. The final ECHO in the parent script is never executed.
Now, let's modify the parent script to test EXIT /B.
@ECHO OFF
ECHO --- [ParentScript] Starting ---
ECHO.
ECHO --- [ParentScript] Calling ChildScript with 'B_Exit' ---
CALL ChildScript.bat B_Exit
ECHO --- [ParentScript] Child script has finished. I can now continue. ---
Run this modified ParentScript.bat from a new command prompt and obtain this output:
--- [ParentScript] Starting ---
--- [ParentScript] Calling ChildScript with 'B_Exit' ---
[ChildScript] Starting up.
[ChildScript] The argument I received was: B_Exit
[ChildScript] Now exiting with EXIT /B...
--- [ParentScript] Child script has finished. I can now continue. ---
The child script terminated, but control returned gracefully to the parent script, which then continued to its final line. The command prompt window remains open.
Passing an Exit Code with EXIT /B
A crucial feature of EXIT /B is its ability to set a specific ERRORLEVEL for the calling script. This is the standard way for a subroutine or a child script to signal success or failure.
Syntax: EXIT /B <ExitCode>
Child Script (ChildWithCode.bat)
ECHO [Child] I will now exit with a specific error code of 5.
EXIT /B 5
Parent Script (ParentChecksCode.bat)
CALL ChildWithCode.bat
ECHO The child script finished with an ERRORLEVEL of: %ERRORLEVEL%
Output:
[Child] I will now exit with a specific error code of 5.
The child script finished with an ERRORLEVEL of: 5
The Modern Best Practice
Because of its predictable and non-destructive behavior, EXIT /B is the recommended best practice for terminating a script in almost all situations.
- It allows for clean, modular scripting where one script can
CALLanother and reliably get control back. - It is the standard way to return a specific exit code.
- It prevents the accidental closure of a user's interactive command prompt.
The only time a plain EXIT is appropriate is when the explicit goal of the script is to close the command window.
Conclusion
The difference between EXIT and EXIT /B is a fundamental concept for controlling the flow of execution in batch scripts.
EXIT: Terminates the entire command prompt session (cmd.exe). Use this when you want to close the window.EXIT /B: Exits only the current batch script. Use this to "return" from a script or subroutine.EXIT /B <N>: Exits the current script and sets theERRORLEVELto<N>.
For writing robust, predictable, and modular scripts, EXIT /B should be your default choice.