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

后端 未结 10 1023
被撕碎了的回忆
被撕碎了的回忆 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 03:09

    AFAIK this come from the C convention that you should return 0 if succeded. See:

    man close
    

    Most of the C (POSIX) api is build like this. http://en.wikipedia.org/wiki/C_POSIX_library

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

    Your trying to equate true/false with success/failure.

    They are two completely, although subtly at first, different dichotomies!

    In shell scripting, there is no such thing as true/false. Shell 'expressions' aren't interpreted as true/false. Rather, shell 'expressions' are processes that either succeed or fail.

    Obviously, a process might fail for many reasons. Thus we need a larger set codes to map possible failures to. The positive integers do the trick. On the other hand, if the process succeeds, that means it did exactly what it was supposed to do. Since there is only one way to do that, we only need one code. 0 does the trick.

    In C, we are creating a program. In a shell script, we are running a bunch of programs to get something done.

    Difference!

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

    Bash is a programming (scripting) language, but it's also a shell and a user-interface. If 0 was error, then the program could only present one kind of error.

    However in Bash, any nonzero value is an error, and we may use any number from 1-255 to represent an error. This means we can have many different kinds of errors. 1 is a general error, 126 means that a file cannot be executed, 127 means 'command not found', etc. Here's a list of Bash Exit Codes With Special Meanings showing some of the most common exit codes.

    There are also many kinds of success (exit status is 0). However, a success will allow you to proceed to the next step—you can like print results to a screen, or execute a command, etc.

    0 讨论(0)
  • The one fundamental point I find important to understand is this. In bash and in unix shells in general, return values are not boolean. They are integer exit codes. As such, you must evaluate them according to the convention saying 0 means success, and other values mean some error.

    With test, [ ] or [[ ]] operators, bash conditions evaluate as true in case of an exit code of 0 (the result of /bin/true). Otherwise they evaluate as false.

    Strings are evaluated differently than exit codes:

    if [ 0 ] ; then echo not null ; fi
    if [ $(echo 0) ] ; then echo not null ; fi
    
    if [ -z "" ] ; then echo null ; fi
    

    The (( )) arithmetic operator interprets 1 and 0 as true and false. But that operator cannot be used as a complete replacement for test, [ ] or [[ ]]. Here is an example showing when the arithmetic operator is useful:

    for (( counter = 0 ; counter < 10 ; counter ++ )) ; do
      if (( counter % 2 )) ; then echo "odd number $counter" ; fi
    done
    
    0 讨论(0)
提交回复
热议问题