如何在 Bash 的 here-doc 中抑制参数扩展?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17839315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I suppress parameter expansion in a here-doc in Bash?
提问by Josh Nankin
I'd like to do the following in bash, but without having variables interpolated:
我想在 bash 中执行以下操作,但没有插入变量:
cat >aBashScript.sh <<EOL
$name
EOL
The file should contain $name
, but instead it's empty. How does one do that?
该文件应包含$name
,但它是空的。如何做到这一点?
回答by Michael
You can disable parameter expansion in here documentsby quoting the limit string:
您可以通过引用限制字符串来禁用此处文档中的参数扩展:
cat >aBashScript.sh <<'EOL'
$name
EOL
回答by Geoffrey
You need to escape the dollar sign, simply prefix it with a backslash to escape it like so:
您需要对美元符号进行转义,只需在其前面加上反斜杠即可转义,如下所示:
cat >aBashScript.sh <<EOL
$name
EOL
Or disable quoting as @Michael suggested.
或者按照@Michael 的建议禁用引用。