使用 cat <<EOL 将字符串写入文件时防止变量替换

Sal*_*ami 3 bash shell heredoc

我想在 bash 脚本中将字符串写入文件,但不知道如何防止变量扩展。一个示例脚本:

#!/bin/bash

cat >./foo.bar <<EOL
t=$(ping 8.8.8.8)
EOL
Run Code Online (Sandbox Code Playgroud)

产生输出:

t=
Pinging 8.8.8.8 with 32 bytes of data:
Reply from 8.8.8.8: bytes=32 time=17ms TTL=57
Reply from 8.8.8.8: bytes=32 time=16ms TTL=57
Reply from 8.8.8.8: bytes=32 time=17ms TTL=57
Reply from 8.8.8.8: bytes=32 time=17ms TTL=57

Ping statistics for 8.8.8.8:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 16ms, Maximum = 17ms, Average = 16ms
Run Code Online (Sandbox Code Playgroud)

所以它似乎$(ping 8.8.8.8)正在被执行并且输出正在被写入文件。如何编写提供的文字字符串而不扩展任何内容?

Pes*_*The 11

cat >./foo.bar <<'EOL'
t=$(ping 8.8.8.8)
EOL
Run Code Online (Sandbox Code Playgroud)

引用Bash 参考手册

[n]<<[-]word

如果 的任何部分word被引用,则分隔符是删除引号的结果word,并且此处文档中的行不会扩展。