Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I've noticed some awk examples which use 1 instead of print to print $0 (eg. To conserve space, I normally use '1'. and on this site).

Is this a documented / safe practice, or is is it subject to breaking in some versions; past, present or future?

Here are some examples:

echo 'a-does-print-$0' | awk '"x"'    
echo 'b-does-print-$0' | awk '$0'   
echo 'c-does-print-$0' | awk '1'
echo 'd-does-print-$0' | awk '(1-2)'  
echo 'd-does-print-$0' | awk '{$0="abc"}1'  

echo 'nothing-prints' | awk '{$0="abc"}'  
echo 'nothing-prints' | awk '$999' 
echo 'nothing-prints' | awk '(1-1)'  
echo 'nothing-prints' | awk '1-1' 
echo 'nothing-prints' | awk '0' 
echo 'nothing-prints' | awk 'unsetVar' 
echo 'nothing-printw' | awk ''

echo 'crashes' | awk '-3.14159'
echo 'crashes' | awk '-2' 
echo 'crashes' | awk '-1'
share|improve this question
add comment

2 Answers

up vote 7 down vote accepted

So...the format of an awk program is a series of EXPRESSION { ACTION } statements. If you omit the ACTION portion, it assumes print, and if you omit the EXPRESSION, it matches everything. So this prints everything:

awk 1

Because your expression (1) evaluates as "true" (and you have no action). This is true for all of your "x-does-print" examples, since given your sample input they all have a non-zero and non-null EXPRESSION and no ACTION. Given an empty input line, $0 in the second example would not output anything.

Likewise, your "nothing-prints" examples other than the first one have an expression that evaluates to "false" (either an empty string or numeric 0). The {$0="abc"} example works differently: it prints nothing because it doesn't have any printing instruction. Given a long input line with at least 999 fields, the second example ($999) would in fact print out the line.

This is fundamental to how awk operates. So sure, it's documented, and it's not going to break.

Note that your "crashes" examples don't actually crash; you've just made a basic mistake in your shell syntax. Awk interprets arguments starting with - as command line options. Quoting them doesn't change anything. If you want to pass awk negative numbers like this, you need to prefix your awk program with -- to indicate to awk that it should stop processing options:

echo 'crashes' | awk -- -3.14159

Will print "crashes", since -3.14150 is neither an empty string nor numeric 0.

share|improve this answer
This doesn't really address the question of why you would do this with awk, since awk 1 is really just a longer way of typing cat. –  larsks Mar 8 '12 at 16:23
larks, you have explained it very well... The simple examples are like cat, but a more "realistic" example is more like this: awk '{$0="abc"}1' ... I originally thought it must have been a true/false thing, but when the negative numbers failed I figured it was time to ask a question... I hadn't considered the possibility of -1 being an arg, but I suppose it's pretty obvious in hindsight :)... Thanks... –  Peter.O Mar 8 '12 at 17:54
Great explanation, only one correction. The awk '' example is more like awk 'BEGIN {}' than it is like awk '0'. The first two will not consume stdin; the third will. Try echo test | { awk ''; echo done; cat; } –  dubiousjim Apr 19 '12 at 18:57
add comment

From the GNU awk manual:

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.

Yes, that usage is portable and future-safe.

Sidenote, this shows your second example,

echo 'b-does-print-$0' | awk '$0'  

will not print every line, only the ones that awk interprets as a true string, i.e. an empty line or a line containing only a way to write the number 0 (0, 00, 0.0e3, -.0, …).

share|improve this answer
Thanks Kevin... It all makes sense now. –  Peter.O Mar 8 '12 at 17:56
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.