exit - Terminate a program

Maintained on
Table of Contents

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>]
OptionDescription
/bEnds 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.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>exit

The same applies inside a batch file. Even if the batch file was invoked with call, cmd.exe ends together with the caller.

When started with `start`

The start command launches a new cmd.exe, so exit in a batch file started with start closes only that new window.

caller.cmd
@echo off
setlocal

call callee.cmd

rem The following command is not executed
pause

endlocal
callee.cmd
@echo off
setlocal

exit

endlocal

After preparing the two files above, run the following command:

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>caller.cmd
おすすめ書籍※ 広告を含む場合があります
中小企業経営者のためのRPA入門 RPA導入を成功させる方法

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

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

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

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

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

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかる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):

callee.cmd
@echo off
setlocal

exit /b

endlocal
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>caller.cmd

caller.cmd continues, so the pause command is executed.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>
caller.cmd
Press any key to continue …
`/b` outside a batch file

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 in exit /b, the current %errorlevel% is kept.
  • Without /b: the value becomes the exit code of the cmd.exe process, which the calling program can read (for example when started with cmd /c).

Rewrite caller.cmd and callee.cmd as follows:

caller.cmd
@echo off
setlocal

call callee.cmd

echo The exit code %errorlevel% has been specified.

pause

endlocal
callee.cmd
@echo off
setlocal

exit /b 100

endlocal
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>caller.cmd

When the above command is executed, the following is displayed:

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>
caller.cmd
The exit code 100 has been specified.
Press any key to continue …

Exit codes can be used for branching with if errorlevel or %errorlevel%. See the following page for details.

Quiz

Practice Problem

What is the purpose of the “exit” command in Command Prompt?

Choose one answer.

  1. Delete files or directories.
  2. End the current Command Prompt session.
  3. Minimize the Command Prompt temporarily.

Practice Problem

Which of the following options correctly sets the specified exit code?

Choose one answer.

  1. exit
  2. exit /b code
  3. exit 100

Practice Problem

Two batch files, caller.cmd and callee.cmd, are prepared, and callee.cmd is called from caller.cmd. If exit without options is executed in callee.cmd, what will be the behavior of caller.cmd?

Choose one answer.

  1. The processing of callee.cmd ends and the processing of caller.cmd continues.
  2. The processing of callee.cmd ends and the processing of caller.cmd also ends.
  3. The processing of callee.cmd continues and the processing of caller.cmd also continues.
#Command Prompt #Batch Files #Arguments #Command Line #Commands