Ubuntu – Why does using double quotes to enclose awk’s action statements produce different results than when using single quotes to enclose them

awkcommand line

If I use single quotes to enclose awk's action statement, it works as expected:

$ dpkg -l | grep "linux\-[a-z]*\-" | awk {'print $2'}
linux-headers-3.13.0-27
linux-headers-3.13.0-27-generic
linux-headers-3.14.4-031404
linux-headers-3.14.4-031404-generic
linux-headers-generic
linux-image-3.13.0-27-generic
linux-image-3.14.4-031404-generic
linux-image-extra-3.13.0-27-generic
linux-image-generic
linux-libc-dev:i386
linux-sound-base

But if I use double quotes to enclose them, it prints the whole line instead of the particular column/field:

$ dpkg -l | grep "linux\-[a-z]*\-" | awk {"print $2"}
ii  linux-headers-3.13.0-27                    3.13.0-27.50                                           all          Header files related to Linux kernel version 3.13.0
ii  linux-headers-3.13.0-27-generic            3.13.0-27.50                                           i386         Linux kernel headers for version 3.13.0 on 32 bit x86 SMP
ii  linux-headers-3.14.4-031404                3.14.4-031404.201405130853                             all          Header files related to Linux kernel version 3.14.4
ii  linux-headers-3.14.4-031404-generic        3.14.4-031404.201405130853                             i386         Linux kernel headers for version 3.14.4 on 32 bit x86 SMP
ii  linux-headers-generic                      3.13.0.27.33                                           i386         Generic Linux kernel headers
ii  linux-image-3.13.0-27-generic              3.13.0-27.50                                           i386         Linux kernel image for version 3.13.0 on 32 bit x86 SMP
ii  linux-image-3.14.4-031404-generic          3.14.4-031404.201405130853                             i386         Linux kernel image for version 3.14.4 on 32 bit x86 SMP
ii  linux-image-extra-3.13.0-27-generic        3.13.0-27.50                                           i386         Linux kernel extra modules for version 3.13.0 on 32 bit x86 SMP
ii  linux-image-generic                        3.13.0.27.33                                           i386         Generic Linux kernel image
ii  linux-libc-dev:i386                        3.13.0-27.50                                           i386         Linux Kernel Headers for development
ii  linux-sound-base                           1.0.25+dfsg-0ubuntu4                                   all          base package for ALSA and OSS sound systems

Why is it so? Why does using double quotes to enclose awk's action statements produce different results than when using single quotes to enclose them? Can I do something so that enclosing using double quotes works like enclosing using single quotes?

Best Answer

If you use double quotes the $2 gets replaced by the shell before awk is called. As $2 is usually empty running

awk {"print $2"}

is the same as running

awk {"print "}

Use

awk {"print \$2"}

if you want to use double quotes.