为什么命令替换会更改带引号的参数的工作方式? [英] Why does command substitution change how quoted arguments work?

查看:45
本文介绍了为什么命令替换会更改带引号的参数的工作方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码段:

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

我希望它会产生:

hello world
world

但是它实际上会产生:

'hello
world'
world

为什么上面的命令与下面的命令不同?

Why is the above command not the same as the following?

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

推荐答案

命令替换后,仅完成了单词拆分和通配符扩展,而没有进行引号处理.

After command substitution, only word splitting and wildcard expansion are done, but not quote processing.

摘自 Bash参考手册

扩展顺序为:大括号扩展,代字号扩展,参数,变量,算术扩展和命令替换(以从左到右的方式完成),单词拆分和文件名扩展.

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.

分词的描述是:

shell扫描参数扩展,命令替换和算术扩展的结果,这些结果在双引号中没有出现,以进行词拆分.

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

shell将$ IFS的每个字符视为定界符,并将其他扩展的结果拆分为这些字符上的单词.如果未设置IFS,或者其值正好是默认的< space>< tab>< newline> ,则< space> < tab> 和< newline> ,并且任何不在开头或结尾的IFS字符序列分隔单词.如果IFS的值不是默认值,则只要单词的开头和结尾都忽略空格字符和制表符的序列,只要空格字符位于IFS值(IFS空格字符)中即可.IFS中不是IFS空格的任何字符,以及任何相邻的IFS空格字符,都会对字段进行定界.IFS空格字符序列也被视为定界符.如果IFS的值为null,则不会发生分词.

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.

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

No mention is made of treating quotes specially when doing this.

这篇关于为什么命令替换会更改带引号的参数的工作方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆