gpt4 book ai didi

bash - 如何在 bash heredocs 中抑制变量替换

转载 作者:行者123 更新时间:2023-11-29 09:00:23 24 4
gpt4 key购买 nike

是否可以创建一个不受变量扩展影响的 heredoc?

例如

cat <<-EOF > somefile.sh
Do not print current value of $1 instead evaluate it later.
EOF

更新 我知道通过 \ 转义。我的实际 heredoc 中有很多变量 - 转义所有变量很容易出错且乏味。

最佳答案

引用分隔符:

cat <<-"EOF"  > somefile.sh
Do not print current value of $1 instead evaluate it later.
EOF

这导致:

$ cat somefile.sh 
Do not print current value of $1 instead evaluate it later.

文档

The format of here-documents is:

          <<[-]word
here-document
delimiter

No parameter and variable expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word. If any characters in word are quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded. If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion, the character sequence \ is ignored, and \ must be used to quote the characters \, $, and `.

If the redirection operator is <<-, then all leading tab characters are stripped from input lines and the line containing delimiter. This allows here-documents within shell scripts to be indented in a natural fashion. [Emphasis added.]

关于bash - 如何在 bash heredocs 中抑制变量替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31645341/

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