truth

Last edit

Summary: updates from markhobley.yi.org

Changed:

< Take care that the case where a string only contains 0 can causes you trouble
< ==== if 0 comes from the data:
< For instance if $1 is
"0" or "000" or " 00", then awk considers $1 as a numeric value and it's false:

to

> The behaviour of awk when a string contains only zeros can be ambiguous, and may even vary depending on which version of awk is being used.
> ==== Taking zero from the data source
> If a value of
"0" or "000" or " 00", is taken from the data source, then awk considers this to be a numeric value and it evaluates to false . The following example shows $1 using a zero value taken from the input data source. The example demonstrates that awk considers $1 to be a numeric value and it evaluates to false:

Changed:

< ==== if you assign "0" to a variable
< {{{ sh
< $ awk 'BEGIN{var="0";if (var) print "hello"}' #same result with var="00"
< hello

to

> ==== Taking zero from a variable
> If a zero value is assigned
to a variable, it may be treated as a non empty string and evaluated as true:
> {{{
> $ awk 'BEGIN{var = "0"; if (var) print "True!"}' # This gives the same result with var = "00"
> True!

Changed:

< Here var if a non empty string, var is thus true.
< With some awk implementation var can be false if used in a numeric context:

to

> However, if the variable was assigned in numeric context (without the quotes), it evaluates as false:
> $ awk 'BEGIN{
var = 0; if (var) print "True!"}' # We get no output because evaluation is false
> ===
With some awk implementations a zero string can be false in numeric context
> With some awk implementations
a zero string can be false in numeric context:

Added:

> === There is no reserved keyword for true or false
> In awk, there is no reserved keyword for true or false. This is a caveat, because true is a valid variable name that is initialized to 0 or "".


Boolean Truth

A boolean [[expression?]] has only two outcomes: true or false. In awk, [[expression?]]s that evaluate to the numeric value 0 or to the empty string are false. The rest is true.

There is not keyword for true or false in awk. This is a [[pitfall?]] because true is a valid variable name that is initialized to 0 or "" and thus awk will not complain:

$ awk 'BEGIN{if (true) {print "true"} else {print "false"}}'
false
ValueTrue/False
0false
1true
-1true
"abc"true
"0"true. it's a non null string, but see below.
""false
" "true a space is still a char

The problems with 0, 00, "0", " 0"....

The behaviour of awk when a string contains only zeros can be ambiguous, and may even vary depending on which version of awk is being used.

Taking zero from the data source

If a value of "0" or "000" or " 00", is taken from the data source, then awk considers this to be a numeric value and it evaluates to false . The following example shows $1 using a zero value taken from the input data source. The example demonstrates that awk considers $1 to be a numeric value and it evaluates to false:

echo 00 | awk'{if ($1) {print "true"}else{print "false"}}'
false # $1 is the number 0

Taking zero from a variable

If a zero value is assigned to a variable, it may be treated as a non empty string and evaluated as true:

$ awk 'BEGIN{var = "0"; if (var) print "True!"}' # This gives the same result with var = "00"
True!

However, if the variable was assigned in numeric context (without the quotes), it evaluates as false:

$ awk 'BEGIN{var = 0; if (var) print "True!"}' # We get no output because evaluation is false

With some awk implementations a zero string can be false in numeric context

With some awk implementations a zero string can be false in numeric context: ie with nawk, oawk on solaris and netbsd awk version 20050424

nawk 'BEGIN{var="0"; foo=var+0;if (var){ print "true"}else{print "false"}}'
false #same result with var="00" and var=" 00"

while with gnu awk, mawk, busybox awk and /usr/xpg4/bin/awk on solaris:

gawk 'BEGIN{var="0"; foo=var+0;if (var){ print "true"}else{print "false"}}'
true #same result with var="00" and var=" 00"

If you want to make sure that your expression is evaluated as a number or as a string you can add 0 or concatenate the null string, respectively:

 var + 0 # a number it will always be false if var contains 0 or "0"
 var == 0 # var is evaluated as a number

 var "" # a string true if non null even if it contains 0
 var = "" 

if you assign 0 to a var

Then var is false, no problem

There is no reserved keyword for true or false

In awk, there is no reserved keyword for true or false. This is a caveat, because true is a valid variable name that is initialized to 0 or "".