gpt4 book ai didi

php - awk 单引号或双引号的用法

转载 作者:可可西里 更新时间:2023-10-31 23:20:26 24 4
gpt4 key购买 nike

为什么 awk 命令使用单引号和双引号产生的结果不同?

root@vm90:/root# who | awk "{ print $2 }"
...
root@vm90:/root# who | awk '{ print $2 }'
...

我想在 PHP shell_exec() 函数中使用 awk,它在整个代码中使用单引号。

这...

$output = shell_exec("who | awk '{ print $2 }'");

...有效,但我更喜欢...

$output = shell_exec('who | awk "{ print $2 }"');

...这个,这是行不通的。

最佳答案

来自GNU Awk manual (强调):

Because certain characters within double-quoted text are processed by the shell, they must be escaped within the text. Of note are the characters $, `, \, and ", all of which must be preceded by a backslash within double-quoted text if they are to be passed on literally to the program. (The leading backslash is stripped first.)

关于php - awk 单引号或双引号的用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23908267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com