EXIT

Close the CMD.EXE session or optionally close only the current batch script (/b). EXIT can also set an ERRORLEVEL.

Syntax
      EXIT [/B] [exitCode]

Key
    /B        When used in a batch script, this option will exit the current script
              or subroutine but not CMD.EXE

   exitCode   Sets the %ERRORLEVEL% to a 32 bit numeric number.
              If quitting CMD.EXE, this will set the process exit code.
              The number can be up to 2147483647.

To close an interactive command prompt, the keyboard shortcut ALT + F4 is an alternative to typing EXIT.

If you CALL one batch file from another and the second script uses an EXIT /B command, then execution of the second script will terminate and the first batch script will continue.

If you CALL (not GOTO) a subroutine within the main batch file and the subroutine uses an EXIT /B command, then execution of the subroutine will terminate and the batch script will continue from the point where it was CALLed (at the next line after the initial CALL).

If EXIT without /B is used within a subroutine, then it will terminate the entire batch script.

If EXIT or EXIT /B are executed directly from the command-line the CMD.exe session will be closed.

Errorlevel

EXIT /b has the option to set a specific exit code, EXIT /b 0 for sucess, EXIT /b 1 (or greater) for an error.

The error code will be returned to the calling process even if SETLOCAL has been set

EXIT without an ExitCode acts the same as goto:eof and will not alter the ERRORLEVEL

n.b. You should never attempt to directly write to the %ERRORLEVEL% system variable, (SET ERRORLEVEL n ) instead use EXIT /b n as a safe way to set the internal ERRORLEVEL to n.

Ctrl-C

An errorlevel of -1073741510 will be interpreted by CMD.exe as a Ctrl-C Key sequence to cancel the current operation, not the entire script which EXIT will do.

To use this in a batch file, launch a new CMD session and immediately exit it, passing this errorlevel. The script will then act as though Ctrl-C had been pressed. Source and examples on DosTips.com.

::Ctrl-C
cmd /c exit -1073741510

When EXIT /b used with FOR /L, the execution of the commands in the loop is stopped, but the loop itself continues until the end count is reached. This will cause slow performance if the loop is (pointlessly) counting up to a large number.
In the case of an infinite loop, this EXIT /b behaviour will cause the script to hang until manually terminated with Ctrl + C

Exiting nested FOR loops, EXIT /b can be used to exit a FOR loop that is nested within another FOR loop.
This will only work if the inner FOR loop is contained in a separate subroutine, so that EXIT /b (or goto:eof) will terminate the subroutine.

EXIT is an internal command.
If Command Extensions are disabled, the EXIT command will still work but may output a spurious 'cannot find the batch label' error.

Examples

Exit if a required file is missing:

@Echo Off
If not exist MyimportantFile.txt Exit /b
Echo If we get this far the file was found

Set the errorlevel to 5:

@Echo Off
Call :setError
Echo %errorlevel%
Goto :eof

:setError
Exit /B 5

Run a batch file which exits with an error and then display it. In these examples throw_err.cmd does nothing other than exit with error #3:

C:\> Echo @exit /b 3 > throw_err.cmd
C:\> CMD /c throw_err.cmd && echo Success || echo Error: %errorlevel%
1 C:\> CMD /K throw_err.cmd && echo Success || echo Error: %errorlevel% C:\> exit 3 C:\> Echo @exit 3 > throw_err.cmd C:\> CMD /c throw_err.cmd && echo Success || echo Error: %errorlevel%
3 From PowerShell (this works with or without /b): PS C:\> ./throw_err.cmd PS C:\> $lastExitCode 3

Use EXIT /b to exit a nested FOR loop (so skipping the values X,Y and Z), but still continue back to the main outer loop:

@Echo Off
Setlocal 
For %%A in (alpha beta gamma) DO (
   Echo Outer loop %%A
   Call :inner 
)
Goto :eof

:inner
For %%B in (U V W X Y Z) DO (
   if %%B==X ( exit /b 2 )
   Echo    Inner loop    Outer=%%A Inner=%%B
)

“Making music is not about a place you go. It’s about a place you get out of. I’m underwater most of the time, and music is like a tube to the surface that I can breathe through. It’s my air hole up to the world. If I didn’t have the music I’d be under water, dead” ~ Fiona Apple

Related commands

VERIFY - Provides an alternative method of raising an error level without exiting.
TSKILL - End a running process.
TITLE - Set the window Title / Display the number of nested CMD sessions.
Equivalent PowerShell: Exit - Exit PowerShell or break - Exit a program loop.
Equivalent bash command (Linux): break - Exit from a loop.


 
Copyright © 1999-2025 windevcluster.com
Some rights reserved