Linux exit status Code and Exit command

Source: Internet
Author: User

Linux provides a special variable $ to save the exit status code of the last executed command. For commands that need to be checked, you must view or use the $? variable immediately after it has been run. Its value becomes the exit status code of the last command executed by the shell:

 Date: +----echo $? 0

By convention, the exit status code for a successful end command is 0. If there is an error at the end of a command, the exit status code is a positive value (1-255):

[[email protected] test]# qwert-echo $? 127

An invalid command returns an exit status code of 127. There are no standards for Linux error exit status codes, but there are some available references, as follows:

Linux Exit Status Code
Status Code Description
0 Command completed successfully
1 General Unknown Error
2 shell commands that are not appropriate
126 Command not enforceable
127 No orders found.
128 Invalid Exit parameters
128+x fatal error associated with Linux signal X
130 command terminated by CTRL + C
255 exit status codes outside the normal range

Exit Status Code 126 indicates that the user does not have the correct permissions to execute the command:

[Email protected] test]#./expect. sh-bash:./expect. SH  Echo $? 126

Another common mistake that comes up is to give a command an invalid argument:

[[Email protected] test]# CD%-BASH:CD:%echo $? 1

This produces a generic exit status code of 1, indicating an unknown error occurred in the command.

By default, the shell script exits with the exit status code of the last command in the script:

sh test. SH  fileecho $? 0

You can change this default behavior and return your exit status code. The Exit command allows you to specify an exit status code at the end of the script:

Cat Test. SH #!/bin/BashA=tenB=C=$[$A + $B]echo   sh test. SH   echo$?  -

This is done directly by using the exit value of the value of C as exit, or by specifying exit 30, which is true, but you should pay attention to this function, because the exit status code can only be 255. See what happens in the following example:

Cat Test. SH #!/bin/BashA=tenB=C=$[$A * $B]echo   sh test. SH   echo$?  -

The exit status code is reduced to the 0~255 interval, and the shell obtains this result through modulo operations. The modulus of a value is the remainder after being removed. The end result is the remainder after the specified number is divided by 256. In this example, the specified value is 300 (the return value) and the remainder is 44, so the remainder is the last state exit code.

Linux exit status Code and Exit command

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.