1

If I execute this in Bash

echo "1 2"

I get 1 2. But if I execute

echo \"1 2\"

I get "1 2".

Now I would figure if I execute

echo $(echo \"1 2\")

I would get 1 2. But again, I get "1 2". In fact, no matter how many command substitutions in the chain

echo $(echo $( ... echo \"1 2\") ... )

I always get "1 2". Why is that?

4

1 回答 1

1

将输出替换$(command)回命令行后,唯一完成的附加解析是分词和通配符扩展。引号不会被处理,因此如果命令输出引号,它们将作为文字字符留在命令行中。

这在 bash manual section on Quote Removal中有解释:

在前面的扩展之后,所有未引用的字符 '\'、''' 和 '"' 的出现均被删除,这些字符不是由上述扩展之一产生的。

由于引号是由命令替换产生的(这是在此之前列出的扩展之一),因此它们不会被删除。

于 2015-02-05T06:19:59.153 回答