exit - Terminate a program

Maintained on

What is the exit command?

The exit command is a command that can be used to terminate batch files and command prompt sessions.

On this page, we explain the basic usage of the exit command and how to set options in an easy-to-understand manner.

How to use the exit command

The basic usage of the exit command is as follows:

exit [/b] <ExitCode>
OptionDescription
/bEnds the processing of the current batch file.
<ExitCode>Specifies a numeric number. If you omit ExitCode, the exit command sets %ERRORLEVEL% to 0.

If you use the exit command without any parameters, it will simply close the command prompt.

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

When you use the exit command without any parameters, the command prompt will close even if it is called by the call or start command.

caller.cmd
@echo off
setlocal

call callee.cmd

rem The following command will not be executed.
pause

endlocal
callee.cmd
@echo off
setlocal

exit

endlocal

After preparing the above two, run the following command:

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

When the above code is executed, the command prompt will immediately exit.

Since the exit command without specifying options is executed in callee.cmd, the pause command in caller.cmd is not executed.

/b option

When the /b option is specified, the processing of the batch file that executed the exit command is terminated.

In the example of caller.cmd and callee.cmd described above, caller.cmd was also terminated when the exit command was executed in the called callee.cmd.

By specifying the /b option, you can terminate only the processing of the called batch file without terminating the processing of the calling batch file.

Rewrite caller.cmd and callee.cmd as follows:

caller.cmd
@echo off
setlocal

call callee.cmd

rem The following command will be executed
pause

endlocal
callee.cmd
@echo off
setlocal

exit /b

endlocal

After preparing the above two, run the following command:

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

When the above command is executed, the pause command will be executed.

×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>
caller.cmd
Press any key to continue …

Specifying an exit code

Specify the exit code as <exit code>.

The exit code can only be a number, and if a non-numeric value is specified, it will not be an error, but the exit code will be treated as 0.

The specified exit code is stored in %errorlevel%.

By using %errorlevel%, you can obtain the exit code of the command that was executed immediately before.

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

After preparing the above two, run the following command:

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

When the above command is executed, the following output will be displayed:

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

Confirmation Test

Practice Problem

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

回答がサーバーに送信されることはありません
Practice Problem

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

回答がサーバーに送信されることはありません
Practice Problem

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

exit
回答がサーバーに送信されることはありません
#Command Prompt #Batch Files #Arguments #Command Line #Commands