防止在反引号中包含的表达式在heredocs中进行评估

use*_*166 15 bash shell

我有这样的文字:

foo bar
`which which`
Run Code Online (Sandbox Code Playgroud)

如果我使用heredoc这样做,我得到一个空白文件:

?  ~  echo <<EOT > out
heredoc> foo bar
heredoc> `which which`
heredoc> EOT
?  ~  cat out

?  ~  
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

编辑

对不起,我打算做cat.问题是它将它写入文件:which: shell built-in command,即评估反推.没有评估,任何方式做到这一点?

有了cat,我明白了

?  ~  cat <<EOT > out
heredoc> foo bar
heredoc> `which which`
heredoc> EOT
?  ~  cat out
foo bar
which: shell built-in command
?  ~  
Run Code Online (Sandbox Code Playgroud)

我不想which which被评估.

dog*_*ane 36

引用标签以防止反引号被评估.

$ cat << "EOT" > out
foo bar
`which which`
EOT

$ cat out
foo bar
`which which`
Run Code Online (Sandbox Code Playgroud)

  • 这为什么有用?Bash没有理智. (17认同)
  • 仅供参考,这也将禁止发生其他bash表达式(例如变量评估).要禁用反引号评估,您可以通过添加反斜杠来逃避反引号,例如'\\`' (7认同)
  • 这里有关于关闭替换的相关文档:http://tldp.org/LDP/abs/html/here-docs.html#EX71C,所以有 3 个选项:`\EOT`、`'EOT'`或“EOT”。 (5认同)