How to use the command "exit" (with examples)
- Windows
- December 25, 2023
The exit
command is used to quit the current CMD instance or the current batch file. It allows you to terminate the command prompt or exit a batch script.
Use case 1: Quit the current CMD instance
Code:
exit
Motivation: This use case is helpful when you want to quickly exit the command prompt without any further action. It allows you to close the command prompt window and return to your normal desktop environment.
Explanation: The exit
command does not require any arguments. When executed, it simply terminates the current CMD instance.
Example output:
C:\Windows\system32>
Use case 2: Quit the current batch script
Code:
exit /b
Motivation: When writing batch scripts, it’s common to include an exit command to gracefully end the script. This use case allows you to exit the current batch script without terminating the entire CMD instance.
Explanation: The /b
argument is used with the exit
command to indicate that only the current batch script should be terminated without affecting the command prompt. It is particularly useful when you have multiple batch scripts running simultaneously.
Example output:
C:\Temp>
Use case 3: Quit using a specific exit code
Code:
exit 2
Motivation: In some cases, you may want to exit a batch script with a specific exit code to indicate success or failure. This use case allows you to set a custom exit code for your batch script.
Explanation: The numeric argument passed to the exit
command indicates the exit code. In this example, the exit code is set to 2
. The exit code can be used by other programs or scripts to determine the outcome of the batch script execution.
Example output:
C:\Projects>
Conclusion:
The exit
command is a versatile tool for terminating the command prompt or batch scripts. Whether you want to quit the current CMD instance, exit a batch script, or set a specific exit code, the exit
command provides these functionalities. By understanding these use cases, you can effectively control the flow and termination of your command prompt sessions and batch scripts.