awk set $1 to itself, effect on $0

jotne posted an interesting thread an hour or two ago, and ended up with the following:

awk '{$1=$1} /^[0-9]+$/' file

I had a question about the effect of $1=$1 assignment, and thought it better to start a new thread, because it's really a different topic.

$ cat test.sh
set -v
echo " abcd" | awk '{        print ":" $0 ":" }'
echo " abcd" | awk '{        print ":" $1 ":" }'
echo " abcd" | awk '{ $0=$0; print ":" $0 ":" }'
echo " abcd" | awk '{ $0=$0; print ":" $1 ":" }'
echo " abcd" | awk '{ $1=$1; print ":" $1 ":" }'
echo " abcd" | awk '{ $0=$1; print ":" $0 ":" }'
echo " abcd" | awk '{ $0=$1; print ":" $1 ":" }'
echo " abcd" | awk '{ $1=$0; print ":" $0 ":" }'
echo " abcd" | awk '{ $1=$0; print ":" $1 ":" }'
echo " abcd" | awk '{ $1=$1; print ":" $0 ":" }'
$ ./test.sh
echo " abcd" | awk '{        print ":" $0 ":" }'
: abcd:
echo " abcd" | awk '{        print ":" $1 ":" }'
:abcd:
echo " abcd" | awk '{ $0=$0; print ":" $0 ":" }'
: abcd:
echo " abcd" | awk '{ $0=$0; print ":" $1 ":" }'
:abcd:
echo " abcd" | awk '{ $1=$1; print ":" $1 ":" }'
:abcd:
echo " abcd" | awk '{ $0=$1; print ":" $0 ":" }'
:abcd:
echo " abcd" | awk '{ $0=$1; print ":" $1 ":" }'
:abcd:
echo " abcd" | awk '{ $1=$0; print ":" $0 ":" }'
: abcd:
echo " abcd" | awk '{ $1=$0; print ":" $1 ":" }'
: abcd:
echo " abcd" | awk '{ $1=$1; print ":" $0 ":" }'
:abcd:

Each one makes perfect sense to me, except for the last one. Could someone explain why $1=$1 results in blanks being removed from $0 var? What is going on behind the scene?

I'm sure this has been discussed before, but I could not find it on searching the forum archives. It disallowed the word "itself". :frowning:

That's used to rebuild the input record $0 .
The following manual entry (for gawk) should make it crystal clear.
Changing Fields - The GNU Awk User's Guide

I had read the documentation before. It did not make it "crystal clear" to me. Of course, that's probably my fault. I'm sure all that documentation is easy to understand for everyone else. :rolleyes: I understood about $0 being rebuilt. Now that I think about it more, it makes sense that when $0 is rebuilt, the only input is $1 so the new version of $0 has no blanks. Thanks.

$0 is rebuilt and the default FS value (a single space) is special in the sense that not only is stands for any spacing, but also leading spacing and closing spacing does not count as a field separator, but gets ignored..