如何在 bash heredocs 中抑制变量替换
How to suppress variable substitution in bash heredocs
是否可以创建一个不受变量扩展影响的 heredoc?
例如
cat <<-EOF > somefile.sh
Do not print current value of instead evaluate it later.
EOF
更新 我知道 \
转义。我的实际 heredoc 中有很多变量 - 转义所有变量很容易出错且乏味。
在 $ 符号前加上反斜杠
$ VAR=XXX
$ cat << END
> dk
> $VAR
> END
dk
$VAR
引用分隔符:
cat <<-"EOF" > somefile.sh
Do not print current value of instead evaluate it later.
EOF
这导致:
$ cat somefile.sh
Do not print current value of 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.]
是否可以创建一个不受变量扩展影响的 heredoc?
例如
cat <<-EOF > somefile.sh
Do not print current value of instead evaluate it later.
EOF
更新 我知道 \
转义。我的实际 heredoc 中有很多变量 - 转义所有变量很容易出错且乏味。
在 $ 符号前加上反斜杠
$ VAR=XXX
$ cat << END
> dk
> $VAR
> END
dk
$VAR
引用分隔符:
cat <<-"EOF" > somefile.sh
Do not print current value of instead evaluate it later.
EOF
这导致:
$ cat somefile.sh
Do not print current value of 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.]