Do escaped double quotes in command substitution become literals in Bash?

徘徊边缘 提交于 2019-12-01 09:47:20

问题


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?


回答1:


After substituting the output of $(command) back into the command line, the only additional parsing that's done is word-splitting and wildcard expansion. Quotes are not processed, so if the command outputs quotes they will be left in the command line as literal characters.

This is explained in the bash manual section on Quote Removal:

After the preceding expansions, all unquoted occurrences of the characters ‘\’, ‘'’, and ‘"’ that did not result from one of the above expansions are removed.

Since the quotes resulted from command substitution (that's one of the expansions listed before this), they're not removed.



来源:https://stackoverflow.com/questions/28337329/do-escaped-double-quotes-in-command-substitution-become-literals-in-bash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!