How to use the exit command in Shell
This article is about how to use the exit command in Shell. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Introduction:
Exit is used to exit the current shell environment process and can return a status code. Generally use $? You can get the status code.
Syntax:
The correct exit syntax exit # returns a status code of 0 by default, which generally means that the command is executed successfully
Error exit syntax exit non-zero numeric # the recommended range of numbers 0-255generally represents a command execution failure
Exit application scenario
1. End the current shell process
two。 When the shell process exits in error, different status values can be returned to represent different errors.
For example, when operating a file in a script file, you can return 1 to indicate that the file does not exist, 2 to indicate that the file does not have read permission, and 3 to indicate that the file type is incorrect.
Instance requirements:
Write a shell script to exit using exit, return a non-zero numeric status value when exiting, execute the script file and print the return status value
Steps:
1. Create an exit.sh file
two。 Edit the exit.sh file to end the current shell with exit digital exit
3. Execute file, print return status value
Demonstrate that vim exit.shalloxambinash binhecho 'hello'exit 2echo' word'# will only output helloecho $? # output 2 to get a summary of the status code returned by the last command execution.
Application scenarios of exit
End the current shell process
Different status codes can be returned for different business processes.
Attached: exit suffix digit function
The exit command is the same as exiting shell and returning the given value. You can terminate the current script execution in a shell script.
Execute exit to cause shell to exit with the specified status value. If the status value parameter is not set, shell exits with a preset value.
A status value of 0 indicates a successful execution, while other values indicate a failure.
0 system stop
1 single user system, no need to log in
2 multi-user system but does not support NFS, command line login
3 complete multi-user mode, command line login
4 unused
5x11 graphic mode, graphical mode login
6 restart the system
Thank you for reading! This is the end of the article on "how to use exit commands in Shell". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!