Scalar values in awk
are either numbers or strings.
gawk
also supports values of type regexp
(see Strongly Typed Regexp Constants).
As described in True and False in awk
, Boolean values in awk
don’t have a separate type: a value counts as “true” if it is nonzero
or non-null, and as “false” otherwise.
When interchanging data with languages that do have a real Boolean type,
using a standard format such as JSON or XML, the lack of a true Boolean
type in awk
is problematic.
(See, for example, the json
extension provided by
the gawkextlib
project.)
It’s easy to import Boolean data into awk
, but then the fact
that it was originally Boolean is lost. Exporting data is even harder;
there’s no way to indicate that a value is really Boolean.
To solve this problem, gawk
provides a function named mkbool()
.
It takes one argument, which is any awk
expression, and it
returns a value of Boolean type.
The returned values are normal awk
numeric values, with
values of either one or zero,
depending upon the truth
value of the original expression passed in the call to mkbool()
.
The typeof()
function (see Getting Type Information) returns
"number|bool"
for these values.
Thus Boolean-typed values are numbers as far as gawk
is concerned, except that extension code can treat them as Booleans
if desired.
While it would have been possible to add two new built-in variables
of Boolean type named TRUE
and FALSE
, doing so would
undoubtedly have broken many existing awk
programs. Instead,
having a “generator” function that creates Boolean values gives
flexibility, without breaking as much existing code.