How to Exit a Script with a Specific Exit Code (EXIT /B)
A well-behaved script doesn't just run and stop; it communicates its outcome. The standard way for any command-line tool or script to report whether it succeeded or failed is by setting an exit code. This numeric value is then stored in the %ERRORLEVEL% variable, allowing other scripts, schedulers, or automation tools to know the result and act accordingly.
This guide will teach you how to use the EXIT /B command to terminate your script and set a custom exit code. You will learn the critical difference between EXIT and EXIT /B, and see a practical example of how a "caller" script can use this information to build a robust workflow.
What is an Exit Code (ERRORLEVEL)?
An exit code is an integer that a program returns to the operating system when it finishes. There is a universal convention for these codes:
0(Zero): The program completed successfully.- Non-Zero (e.g.,
1,2,100): The program encountered an error or failed. Different non-zero values can be used to indicate different types of errors.
Automation tools like Windows Task Scheduler, Jenkins, or other batch scripts check this exit code (%ERRORLEVEL%) to determine if a step in a process has succeeded or failed.
The Core Command: EXIT /B [ExitCode]
The EXIT command terminates the script. The /B switch is the most important part of this command for scripting.
The Syntax: EXIT /B [ExitCode]
EXIT: The command to terminate./B: This crucial switch means "exit the current Batch script only." It returns control to the calling script or to the interactive command prompt.[ExitCode]: An optional integer that you want to set as the script's exit code. If you omit it, the script will exit with the current value of%ERRORLEVEL%.
Basic Example: Exiting with Success and Failure Codes
Let's create a script that can exit in two different ways based on its input.
@ECHO OFF
IF /I "%1"=="SUCCESS" (
ECHO The task was successful.
EXIT /B 0
)
IF /I "%1"=="FAIL" (
ECHO The task failed.
EXIT /B 1
)
ECHO Invalid argument. Please use 'SUCCESS' or 'FAIL'.
EXIT /B 99
We run the script and then immediately check the %ERRORLEVEL% variable.
C:\> MyTask.bat SUCCESS
The task was successful.
C:\> ECHO %ERRORLEVEL%
0
C:\> MyTask.bat FAIL
The task failed.
C:\> ECHO %ERRORLEVEL%
1
C:\> MyTask.bat something_else
Invalid argument. Please use 'SUCCESS' or 'FAIL'.
C:\> ECHO %ERRORLEVEL%
99
How Calling Scripts Use the Exit Code
Setting an exit code is only useful if another process checks it. Here is a "manager" script that calls our previous MyTask.bat and acts on the result.
@ECHO OFF
ECHO --- Calling MyTask.bat ---
CALL MyTask.bat FAIL
IF %ERRORLEVEL% EQU 0 (
ECHO The task reported success.
) ELSE (
ECHO The task reported a failure. Exit code was: %ERRORLEVEL%
)
ECHO --- Another way: using && and || ---
CALL MyTask.bat SUCCESS && (ECHO Task succeeded.) || (ECHO Task failed.)
EXIT vs. EXIT /B: A Critical Distinction
This is one of the most important concepts for writing well-behaved scripts.
-
EXIT /B: Exits the current batch script. If you run the script from an interactive command prompt, control returns to the prompt. This is what you want 99% of the time. -
EXIT(without/B): Terminates the entirecmd.exeprocess. If a user is running your script from an interactive command prompt, usingEXITwill cause the entire command window to close abruptly. This is very user-unfriendly and should almost never be used.
Rule of Thumb: Unless you have a very specific reason to close the user's console window, always use EXIT /B.
Common Pitfalls and How to Solve Them
- Forgetting
/B: As mentioned above, this is a major pitfall that leads to a poor user experience. Always use/B. - Non-Integer Exit Codes: The
[ExitCode]must be a signed integer. You cannot return a string likeEXIT /B "Error". - Implicit Exits: A script that "falls off the end" (reaches the end of the file) will implicitly exit. The
ERRORLEVELin this case will be the exit code of the last command that was run. It's a best practice to have an explicitEXIT /B 0at the end of a successful script path to guarantee a success code.
Practical Example: A File Validation Script
This script (CheckFile.bat) checks a file and returns a specific exit code for each possible outcome. A second script (RunCheck.bat) calls it and interprets the result.
Script 1 (CheckFile.bat)
@ECHO OFF
SET "FilePath=%~1"
IF "%FilePath%"=="" ( EXIT /B 1 )
IF NOT EXIST "%FilePath%" ( EXIT /B 2 )
FOR %%F IN ("%FilePath%") DO (
IF %%~zF EQU 0 ( EXIT /B 3 )
)
EXIT /B 0
Script 2 (RunCheck.bat)
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
CALL CheckFile.bat "my_data.csv"
SET "ExitCode=%ERRORLEVEL%"
ECHO Checking "my_data.csv" returned exit code: !ExitCode!
IF !ExitCode! EQU 0 ECHO [STATUS] File is valid.
IF !ExitCode! EQU 1 ECHO [STATUS] Error: No filename was provided.
IF !ExitCode! EQU 2 ECHO [STATUS] Error: File does not exist.
IF !ExitCode! EQU 3 ECHO [STATUS] Error: File is empty.
Conclusion
Using EXIT /B to set a specific exit code is the standard and professional way for a batch script to communicate its final status. It is the foundation of building modular, interconnected automation.
Key takeaways:
- An exit code of
0means success, while a non-zero value means failure. - The command to use is
EXIT /B [ExitCode]. - Always use the
/Bswitch to prevent your script from closing the user's command prompt window. - Other scripts and tools can check
%ERRORLEVEL%or use conditional operators (&&and||) to react to the outcome of your script.