简体   繁体   中英

Why Does Running Awk With Double Quotes Break But Works With Single Quotes?

I noticed when running a command that this statement doesn't recognize the delimiter

awk -F',' "{print $4}" wtd.csv

However, this one does.

awk -F',' '{print $4}' wtd.csv

Any reason why? I'm sure this is part of some general bash rule I'm forgetting.

If you're using double quotes, $4 will get replaced by Bash (probably with the empty string). You'd need to escape the $ to use it in double quotes.

Example where this also is happening:

[thom@lethe ~]$ echo '$4'
$4
[thom@lethe ~]$ echo "$4"

[thom@lethe ~]$ echo "\$4"
$4

You are forgetting that double-quotes allow bash variable interpolation. In this case it tries to replace $4 with the fourth argument to the shell which is usually empty.

The single-quotes prevent bash interpolation and passes the literal $4 to awk.

You'll have identical results with:

awk -F',' '{print $4}' wtd.csv
awk -F',' "{print \$4}" wtd.csv

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM