我正在尝试将多行字符串重定向到文件.该字符串包含像quota这样的特殊字符,如下所示:
import "this"
import "that"
main() {
println("some text here");
}
Run Code Online (Sandbox Code Playgroud)
我可以使用echo
如下:
echo "import \"this\"
import \"that\"
main() {
println(\"some text here\");
}" > myfile.txt
Run Code Online (Sandbox Code Playgroud)
这种方法的问题是我需要逃避所有配额字符.我想过cat
用来消除转义的必要性.它在交互式shell中运行良好,这样我就可以键入cat > myfile.txt
然后编写我的字符串而不转义,然后用文本结束标记<control>d
.
如何EOF
在没有实际键序列的情况下在脚本中进行标记<control>d
?
使用'here document':
cat <<'EOF' > myfile.txt
import "this"
import "that"
main() {
println("some text here");
}
EOF
Run Code Online (Sandbox Code Playgroud)
初始'EOF'周围的引号意味着"不要在这里扩展任何shell变量(等)".