jav*_*dba 6 syntax bash heredoc
是否有可能创建一个不受变量扩展影响的heredoc?
例如
cat <<-EOF > somefile.sh
Do not print current value of $1 instead evaluate it later.
EOF
Run Code Online (Sandbox Code Playgroud)
更新 我知道逃避\
.我的实际heredoc中有很多变量 - 它很容易出错并且很难逃脱所有这些变量.
引用分隔符:
cat <<-"EOF" > somefile.sh
Do not print current value of $1 instead evaluate it later.
EOF
Run Code Online (Sandbox Code Playgroud)
这导致:
$ cat somefile.sh
Do not print current value of $1 instead evaluate it later.
Run Code Online (Sandbox Code Playgroud)
here-documents的格式是:
Run Code Online (Sandbox Code Playgroud)<<[-]word here-document delimiter
不对字执行参数和变量扩展,命令替换,算术扩展或路径名扩展. 如果引用单词中的任何字符,则分隔符是单词上的引号删除的结果,并且不会展开here-document中的行. 如果word不加引号,则here-document的所有行都要进行参数扩展,命令替换和算术扩展,忽略字符序列\,并且必须使用\来引用字符\,$和`.
如果重定向运算符是<< - ,则从输入行和包含分隔符的行中删除所有前导制表符.这允许shell脚本中的文档以自然的方式缩进.[重点补充.]