exit - Terminate a program
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>
Option | Description |
---|---|
/b | Ends 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.
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.
@echo off
setlocal
call callee.cmd
rem The following command will not be executed.
pause
endlocal
@echo off
setlocal
exit
endlocal
After preparing the above two, run the following command:
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:
@echo off
setlocal
call callee.cmd
rem The following command will be executed
pause
endlocal
@echo off
setlocal
exit /b
endlocal
After preparing the above two, run the following command:
When the above command is executed, the pause
command will be executed.
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:
@echo off
setlocal
call callee.cmd
echo The exit code %errorlevel% has been specified.
pause
endlocal
@echo off
setlocal
exit /b 100
endlocal
After preparing the above two, run the following command:
When the above command is executed, the following output will be displayed: