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

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

提交回复
热议问题