Bash 在使用反引号时提示 here-document 中的语法错误

标签 bash cat quoting backticks

我正在运行以下一段 bash 代码:

cat << END_TEXT
       _             _ 
      | |           | |
  __ _| |__   ___ __| |
 / _` | '_ \ / __/ _` |
| (_| | |_) | (_| (_| |
 \__,_|_.__/ \___\__,_|
END_TEXT

出现错误:

bash: command substitution: line 1: syntax error near unexpected token `|'
bash: command substitution: line 1: ` | '_ \ / __/ _'

最佳答案

无需转义反引号。只需使用引用的 here-doc 字符串作为:

cat <<-'END_TEXT'
        _             _
       | |           | |
   __ _| |__   ___ __| |
  / _` | '_ \ / __/ _` |
 | (_| | |_) | (_| (_| |
  \__,_|_.__/ \___\__,_|
END_TEXT

根据 man bash :

If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion, the character sequence \<newline> is ignored, and \ must be used to quote the characters \, $, and `.

关于Bash 在使用反引号时提示 here-document 中的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48745627/

相关文章:

linux - 在bash中提取没有路径和扩展名的文件基名

linux - "-"在此 linux 命令中意味着什么?

javascript - 用于将引用语法转换为 HTML 的正则表达式

Java 正则表达式 : Look behind group does not have an "obvious maximum length"

bash - 在 bash 脚本中使用 curl 时保留换行符

linux - 如何使用 bash 脚本替换单引号中的值?

python - 如何在 papermill 中参数化 Python 字典?

node.js - 在 Node 项目上运行 make 命令

linux - 在 shell 脚本中使用 for 循环遍历参数列表?

linux - 将 3000k 个 txt 文件合并成一个 txt 文件 Linux