cat一个shell函数内的文件

Qia*_* Li 5 shell

我想知道如何实现以下内容:

test(){
  cat>file<<'EOF'
    abc
  EOF
}
Run Code Online (Sandbox Code Playgroud)

非常感谢.

Tyl*_* A. 10

强:

删除EOF前面的空格(因此它本身就在一条线上而不是缩进).


sar*_*old 5

来自bash(1):

If the redirection operator is <<-, then all leading tab
characters are stripped from input lines and the line
containing delimiter.  This allows here-documents within
shell scripts to be indented in a natural fashion.
Run Code Online (Sandbox Code Playgroud)

它说tab,在我的测试中,tab工作,但空间不:

#!/bin/bash

cat>file <<-END
    hello
    world
    hello
    END

echo done
Run Code Online (Sandbox Code Playgroud)

(所有这些缩进都是制表符;关于代码的四个前导空格标记的有趣之处在于,在渲染文本中也只显示四个空格.)