为什么0是真实的,但假的就是1中的壳呢? [英] Why 0 is true but false is 1 in the shell?
问题描述
false; echo $?
以上将输出 1
,这是矛盾与我知道了。
The above will output 1
, which is contradictory with all other programming languages I know.
任何理由呢?
推荐答案
这是一个惯例,而是一个特别有用的之一,当你去想它。一般来说,如果一个程序成功,这就是你需要知道的。如果失败了,但是,您可能需要了解各种有关失败的信息 - 为什么会发生,如何解决它,等有均值为零成功和非零意味着失败让你可以检查pretty容易成功,并探讨更多细节的特定错误,如果你想。很多API和框架也有类似的约定 - 即成功返回0和功能的,失败给回一个错误code描述特定失败案例
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是真实的,但假的就是1中的壳呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!