Why 0 is true but false is 1 in the shell?

后端 未结 10 1022
被撕碎了的回忆
被撕碎了的回忆 2020-11-28 02:29
false; echo $?

The above will output 1, which is contradictory with all other programming languages I know.

Any reason in this

相关标签:
10条回答
  • 2020-11-28 02:53

    it's a convention dated back to the early days of Unix.

    By convention, all system calls return 0 if succeed, non-zero otherwise, because then different numbers can be used to indicate different reason of failure.

    Shells follow this convention, 0 means last command succeeded, non-zero otherwise. Similarly, the non-zero return value comes in handy for output error messages: e.g. 1: "brain dead", 2: "heartless", and so on.

    0 讨论(0)
  • 2020-11-28 02:59

    It's just a convention that a 0 exit code means success. EXIT_SUCCESS will be 0 on almost every modern system.

    EDIT:

    "why both test 0 and test 1 returns 0(success) ?"

    That's a completely different question. The answer is that passing a single argument to test always results in success unless that argument is the null string (""). See the Open Group documentation.

    0 讨论(0)
  • 2020-11-28 02:59

    Typically programs return zero for success, non-zero for failure; false returns 1 because it's a convenient non-zero value, but generally any non-zero value means failure of some sort, and many programs will return different non-zero values to indicate different failure modes

    0 讨论(0)
  • 2020-11-28 03:01

    Maybe a good way to remember it is :

    • Return codes answer "what's the answer?" true (or other result) or false
    • Exit codes answer "what's the problem?" exit code or no problem (0)
    0 讨论(0)
  • 2020-11-28 03:04

    There are two related issues here.

    First, the OP's question, Why 0 is true but false is 1 in the shell? and the second, why do applications return 0 for success and non-zero for failure?

    To answer the OP's question we need to understand the second question. The numerous answers to this post have described that this is a convention and have listed some of the niceties this convention affords. Some of these niceties are summarized below.

    Why do applications return 0 for success and non-zero for failure?

    Code that invokes an operation needs to know two things about the exit status of the operation. Did the operation exit successfully? [*1] And if the operation does not exit successfully why did the operation exit unsuccessfully? Any value could be used to denote success. But 0 is more convenient than any other number because it is portable between platforms. Summarizing xibo's answer to this question on 16 Aug 2011:

    Zero is encoding-independent.

    If we wanted to store one(1) in a 32-bit integer word, the first question would be "big-endian word or little-endian word?", followed by "how long are the bytes composing a little-endian word?", while zero will always look the same.

    Also it needs to be expected that some people cast errno to char or short at some point, or even to float. (int)((char)ENOLCK) is not ENOLCK when char is not at least 8-bit long (7-bit ASCII char machines are supported by UNIX), while (int)((char)0) is 0 independent of the architectural details of char.

    Once it is determined that 0 will be the return value for success, then it makes sense to use any non-zero value for failure. This allows many exit codes to answer the question why the operation failed.

    Why 0 is true but false is 1 in the shell?

    One of the fundamental usages of shells is to automate processes by scripting them. Usually this means invoking an operation and then doing something else conditionally based on the exit status of the operation. Philippe A. explained nicely in his answer to this post that

    In bash and in unix shells in general, return values are not boolean. They are integer exit codes.

    It's necessary then to interpret the exit status of these operations as a boolean value. It makes sense to map a successful (0) exit status to true and any non-zero/failure exit status to false. Doing this allows conditional execution of chained shell commands.

    Here is an example mkdir deleteme && cd $_ && pwd. Because the shell interprets 0 as true this command conveniently works as expected. If the shell were to interpret 0 as false then you'd have to invert the interpreted exit status for each operation.

    In short, it would be nonsensical for the shell to interpret 0 as false given the convention that applications return 0 for a successful exit status.


    [*1]: Yes, many times operations need to return more than just a simple success message but that is beyond the scope of this thread.

    See also Appendix E in the Advanced Bash-Scripting Guide

    0 讨论(0)
  • 2020-11-28 03:08

    It's a convention, but a particularly useful one when you think about it. In general, if a program succeeds that's all you need to know. If it fails, however, you might need to know all kinds of information about the failure - why it happened, how to fix it, etc. Having zero mean 'success' and non-zero mean failure lets you can check pretty easily for success, and investigate the particular error for more details if you want to. A lot of APIs and frameworks have a similar convention - functions that succeed return 0 and and those that fail give back an error code describing the particular failure case.

    0 讨论(0)
提交回复
热议问题