exit - Terminate a program
What is the exit command?
The exit command ends Command Prompt (cmd.exe) or a batch file. You can also specify an exit code when exiting.
This page explains the basic usage of the exit command, how /b changes its behavior, and how exit codes work, with sample code.
How to use the exit command
exit [/b] [<ExitCode>]
| Option | Description |
|---|---|
/b | Ends only the batch file (or subroutine) that ran exit, without closing cmd.exe. |
<ExitCode> | Specifies a numeric exit code. See “Specifying an exit code” below for what happens when it is omitted. |
exit without options
Without options, exit terminates cmd.exe itself, and the Command Prompt window closes.
The same applies inside a batch file. Even if the batch file was invoked with call or start /wait, cmd.exe ends together with the caller.
@echo off
setlocal
call callee.cmd
rem The following command is not executed
pause
endlocal
@echo off
setlocal
exit
endlocal
After preparing the two files above, run the following command:

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超 入門

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門
Because exit in callee.cmd ends cmd.exe, the pause in caller.cmd is never executed and the Command Prompt window closes.
The /b option
With /b, cmd.exe stays open. Only the batch file that ran exit /b ends, and control returns to the caller.
Rewrite callee.cmd as follows (caller.cmd is unchanged):
@echo off
setlocal
exit /b
endlocal
caller.cmd continues, so the pause command is executed.
If you run exit /b outside a batch file (typed directly at the prompt), cmd.exe exits.
Specifying an exit code
Specify the exit code as a number in <ExitCode>.
- With
/b: the value is set to%errorlevel%and can be read by the calling batch file. If you omit the number, as inexit /b, the current%errorlevel%is kept. - Without
/b: the value becomes the exit code of thecmd.exeprocess, which the calling program can read (for example when started withcmd /c).
Rewrite caller.cmd and callee.cmd as follows:
@echo off
setlocal
call callee.cmd
echo The exit code %errorlevel% has been specified.
pause
endlocal
@echo off
setlocal
exit /b 100
endlocal
When the above command is executed, the following is displayed:
Exit codes can be used for branching with if errorlevel or %errorlevel%. See the following page for details.