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

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

提交回复
热议问题