The command: "EXIT" is on Windows 12, 11, 10, .. , MS Server 2025, 2022, 2019, .. available
The examples for the command "EXIT"
The
`EXIT` command in Windows Command Prompt is used to terminate the execution of a batch script or a Command Prompt session. Here are some examples:
Example 1: End batch script:
@ECHO OFF
ECHO The batch script exits.
EXIT
Description: The batch script is terminated with the
`EXIT` command. The text below will no longer be executed.
Example 2: Conditionally exiting the batch script:
@ECHO OFF
SET condition=1
IF %Condition% EQU 1 (
ECHO The condition is met. The script ends.
EXIT
)
ECHO The script continues.
Description: The batch script checks a condition and exits with
`EXIT` if the condition is met.
Example 3: Exit with a specific exit code:
@ECHO OFF
ECHO The batch script exits.
EXIT /B 5
Description: The batch script exits with exit code 5. This exit code can be obtained in a parent script or a command prompt session.
Example 4: Using EXIT in a loop:
@ECHO OFF
FOR /L %%A IN (1, 1, 3) DO (
ECHO loop iteration %%A
IF %%A EQU 2 (
ECHO End the loop.
EXIT
)
)
Description: The batch script executes a loop and exits early with
`EXIT` if the loop condition is met.
Example 5: EXIT in a function:
@ECHO OFF
CALL :ExampleFunction
ECHO The batch script continues.
GOTO :EOF
:ExampleFunction
ECHO The function is ended.
EXIT /B
Description: The batch script calls a function and exits within the function with
`EXIT /B`. The function call then continues.
Note: Note that
`EXIT /B` is used to exit only the current batch script or job. If
`EXIT /B` is not used, the command ends the entire command prompt session.
It is important to note that the exit code is usually 0 if the script completes without problems. An exit code greater than 0 usually indicates an error. Exit codes can be queried in batch scripts or from higher-level processes to monitor the success or failure of a script.
"EXIT" Excerpt from Microsoft Windows Help
Microsoft Windows [Version 10.0.19045.3693]
(c) Copyright 1985-2023 Microsoft Corp.
C:\\WINDOWS>
Quits the CMD.EXE program (command interpreter) or the current batch
script.
EXIT [/B] [exitCode]
/B specifies to exit the current batch script instead of
CMD.EXE. If executed from outside a batch script, it
will quit CMD.EXE
exitCode specifies a numeric number. if /B is specified, sets
ERRORLEVEL that number. If quitting CMD.EXE, sets the process
exit code with that number.
Important information, tips for the "EXIT" command
There are a few important points to note when using the
`EXIT` command in the Windows Command Prompt:
1.
Exit Codes: The
`EXIT` command can optionally return an exit code. The default exit code is 0, which usually indicates successful completion. Exit codes greater than 0 can be used to indicate error conditions or various outcomes.
EXIT /B 5
2.
Use in Functions: When you use
`EXIT` in a function, the batch script or the caller will exit. Use
`EXIT /B` to just exit the function and continue execution in the batch script.
CALL :ExampleFunction
ECHO The batch script continues.
GOTO :EOF
:ExampleFunction
ECHO The function is ended.
EXIT /B
3.
Return to parent command prompt: If you use
`EXIT` without
`/B`, not only the batch script but the entire command prompt session will be terminated. This may cause you to lose the command prompt.
EXIT
4.
Exit Codes and Error Monitoring: Exit codes can be monitored in scripts or by higher-level processes. This is particularly useful for responding to errors or different execution paths.
5.
Determination of current exit code: You can get the last exit code in the command prompt using the
`%ERRORLEVEL%` command.
ECHO The last exit code was: %ERRORLEVEL%
6.
Exiting Loops: You can use
`EXIT` to exit a loop early. However, note that this can terminate the entire batch session if
`EXIT` is used without
`/B`.
FOR /L %%A IN (1, 1, 5) DO (
ECHO loop iteration %%A
IF %%A EQU 3 (
ECHO End the loop.
EXIT
)
)
It is important to ensure that
`EXIT` is used judiciously to avoid unintentionally terminating command prompt sessions or scripts. Particularly when using
`EXIT` in scripts that are called from other scripts, care should be taken to maintain the expected behavior.