How can I write a heredoc to a file in Bash script?

前端 未结 9 1005
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 12:33

How can I write a here document to a file in Bash script?

9条回答
  •  逝去的感伤
    2020-11-22 13:05

    If you want to keep the heredoc indented for readability:

    $ perl -pe 's/^\s*//' << EOF
         line 1
         line 2
    EOF
    

    The built-in method for supporting indented heredoc in Bash only supports leading tabs, not spaces.

    Perl can be replaced with awk to save a few characters, but the Perl one is probably easier to remember if you know basic regular expressions.

提交回复
热议问题