使用$()将的输出分配cat给您的变量,如下所示:VAR=$(cat <<'END_HEREDOC'abc'asdf"$(dont-execute-this)foo"bar"''END_HEREDOC)# this will echo variable with new lines intactecho "$VAR"# this will echo variable without new lines (changed to space character)echo $VAR确保以单引号分隔END_HEREDOC开头。请注意,此行末尾定界符END_HEREDOC必须在行上单独存在(因此,圆括号末尾位于下一行)。