[英]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.
我确定这是我忘记的一些常规bash规则的一部分。
If you're using double quotes, $4
will get replaced by Bash (probably with the empty string). 如果您使用双引号,则
$4
将被Bash替换(可能是空字符串)。 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. 您忘记了双引号允许bash变量插值。 In this case it tries to replace
$4
with the fourth argument to the shell which is usually empty. 在这种情况下,它将尝试用外壳程序的第四个参数(通常为空)替换
$4
。
The single-quotes prevent bash interpolation and passes the literal $4
to awk. 单引号防止bash插值,并将文字
$4
传递给awk。
You'll have identical results with: 您将获得与以下结果相同的结果:
awk -F',' '{print $4}' wtd.csv
awk -F',' "{print \$4}" wtd.csv
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.