exit /b in Windows batch script

You must be familiar with the exit command. If you execute this command in a DOS window, the DOS window closes and the cmd process exits. This is like closing the window by clicking the “Close” button on the title bar of the cmd window.

exit /b is often seen in a Windows bat file. When the script runs to the “exit /b” line,  the commands after this line are not executed and the commands after the calling of current script continue. If current script was run/called directly by the cmd process, the cmd process won’t exit. This is exactly the aim of the /b(b means batch) switch. Without the /b switch, the script exits and the cmd running the script exits too. If you run exit /b in the cmd window(not in a bat script), the cmd exits.

You can provide an exit code to the exit command, i.e., exit [/b] exitCode. If the cmd process exits(as in the case you run the exit command in the cmd window or use “exit exitCode” in a bat script), the exitcode will be set to the exit code of the cmd process. If you use exit /b exitCode in a batch file, since the cmd does not end when the script ends, there would be no exit code to set, in that case, the ERRORLEVEL environment variable of the cmd process is set to the exitCode.

 

If you like my content, please consider buying me a coffee. Buy me a coffeeBuy me a coffee Thank you for your support!
Posted in

Leave a Reply