Next: , Up: Truth Values and Conditions   [Contents][Index]


6.3.1 True and False in awk

Many programming languages have a special representation for the concepts of “true” and “false.” Such languages usually use the special constants true and false, or perhaps their uppercase equivalents. However, awk is different. It borrows a very simple concept of true and false from C. In awk, any nonzero numeric value or any nonempty string value is true. Any other value (zero or the null string, "") is false. The following program prints ‘A strange truth value’ three times:

BEGIN {
   if (3.1415927)
       print "A strange truth value"
   if ("Four Score And Seven Years Ago")
       print "A strange truth value"
   if (j = 57)
       print "A strange truth value"
}

There is a surprising consequence of the “nonzero or non-null” rule: the string constant "0" is actually true, because it is non-null. (d.c.)