简体   繁体   English

awk 与 /bin/bash -c 一起使用时不起作用

[英]awk not working when used with /bin/bash -c

This correctly prints test这正确打印test

$ echo 'this is a test' | awk '{print $4}'
test

While using this command inside /bin/bash -c does not work/bin/bash -c中使用此命令时不起作用

/bin/bash -c "echo 'this is a test' | awk '{print $4}'"
this is a test

How can I get awk to work correctly when using with /bin/bash -c ?/bin/bash -c一起使用时,如何让 awk 正常工作?

$4 is expended by shell since you have double quoted command string. $4由 shell 花费,因为您有双引号命令字符串。

You can check trace output by adding -x in bash command line:您可以通过在bash命令行中添加-x来检查跟踪 output:

bash -xc "echo 'this is a test' | awk '{print $4}'"
+ echo 'this is a test'
+ awk '{print }'
this is a test

Since $4 expands to an empty string it effectively runs awk '{print }' and thus complete line is printed in output.由于$4扩展为一个空字符串,它有效地运行awk '{print }' ,因此完整的行打印在 output 中。

To fix this, you should be using an escaped $ to avoid this expansion:要解决此问题,您应该使用转义的$来避免这种扩展:

bash -c "echo 'this is a test' | awk '{print \$4}'"
test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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