“0” (zero) means True/Pass/Success in BASH, unless it doesn’t!

A well considered policy of many founders of programming culture was to make True/Pass/Successs/OK a “0” and any error condition non-zero, for any routine/function/procedure/method/library element which returned a status. There are plenty of good reasons for this.
1) zero vs non-zero tests are very likely to exist at a very fundamental level (ie single instruction referring to a single input value) on pretty much any computer hardware. So machine code generated from the test of a return code would be fast and compact. Both good things when you want to encourage people to return meaningful status.
2) A wide range of possible error returns encourages the author to give some thought to error detection. It also makes it easy to indicate, generally, what the error is. How very structured!
3) Multiple errors could be returned in a single non-zero value by or’ing different bit values togethter. Even more structured.
4) With return values from calls established, Boolean variables and the lowest level “If for while until” tests are readily defined.

The problem with this is that the equally compelling case for 0=False/Fail/Nope/Faghetaboudit exists, and the single-instruction-single operand argument for zero/non-zero works just as well if zero is FAIL as it if it PASS. In further cases:
2) Non-zero being true and zero being false are intuitive and natural. if ( ptr ) is a steady and reassuring rock when you’re writing your first pointer code. Each time you use it or see it, you reflect again that a pointer is a non-zero value and a zero value means the pointer has not been set. Java fans may argue that you shouldn’t have pointers in the first place but that’s still an open issue as far as I can see.

Anyway, while the C programming language takes the 0=false intuitive approach, BASH, an ostensibly even LOWER level language, takes the 0=true path. And most delightfully, the “expr” program which does math for BASH follows C, not BASH. So, for example,

`expr “$potentialMatches” = “$traceBlkIdx”`

returns 1 if the two integer values are identical, and 0 if they are not. The obvious work-around is to make it != instead of =, but that looks wrong to me, so it needs a comment to tell the reader that its not an error. So in the end, I settled on subtracting one from the other, which gives the BASH logical 0 = TRUE = the numbers are equal and non-zero = FALSE = the numbers are NOT equal. I still put in a comment but to explain why I’m using a somewhat round-about evaluation and why. Anyone looking at it will know that two equal numbers will have a difference of 0, and thus be reminded that TRUE is 0 in BASH.

What could have been a nit of confusion becomes a minor tutorial on a place where presumptions are not aligned.

What a hoot!

`expr “$potentialMatches” – “$traceBlkIdx”`


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s