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

后端 未结 10 1108
被撕碎了的回忆
被撕碎了的回忆 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条回答
  •  猫巷女王i
    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.

提交回复
热议问题