gpt4 book ai didi

bash - 为什么命令替换会改变引用参数的工作方式?

转载 作者:行者123 更新时间:2023-11-29 08:51:44 24 4
gpt4 key购买 nike

我有以下片段:

printf "%s\n%s\n%s" $(echo "'hello world' world")

我希望它产生:

hello world
world

但它实际上产生:

'hello
world'
world

为什么上面的命令和下面的不一样?

printf "%s\n%s\n%s" 'hello world' world

最佳答案

命令替换后只做分词和通配符扩展,不做引用处理。

来自 tbe Bash Reference Manual

The order of expansions is: brace expansion, tilde expansion, parameter, variable, and arithmetic expansion and command substitution (done in a left-to-right fashion), word splitting, and filename expansion.

分词的描述是:

The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting.

The shell treats each character of $IFS as a delimiter, and splits the results of the other expansions into words on these characters. If IFS is unset, or its value is exactly <space><tab><newline>, the default, then sequences of <space>, <tab>, and <newline> at the beginning and end of the results of the previous expansions are ignored, and any sequence of IFS characters not at the beginning or end serves to delimit words. If IFS has a value other than the default, then sequences of the whitespace characters space and tab are ignored at the beginning and end of the word, as long as the whitespace character is in the value of IFS (an IFS whitespace character). Any character in IFS that is not IFS whitespace, along with any adjacent IFS whitespace characters, delimits a field. A sequence of IFS whitespace characters is also treated as a delimiter. If the value of IFS is null, no word splitting occurs.

没有提到在执行此操作时特别处理引号。

关于bash - 为什么命令替换会改变引用参数的工作方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18567685/

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