开发者

How to append several lines of text in a file using a shell script

开发者 https://www.devze.com 2023-02-10 20:47 出处:网络
I want to write several lines (5 or more) to a file I\'m going to create in script. I can do this by echo >> filename. But I would like to know what the best way t开发者_开发知识库o do this?You can us

I want to write several lines (5 or more) to a file I'm going to create in script. I can do this by echo >> filename. But I would like to know what the best way t开发者_开发知识库o do this?


You can use a here document:

cat <<EOF >> outputfile
some lines
of text
EOF


I usually use the so-called "here-document" Dennis suggested. An alternative is:

(echo first line; echo second line) >> outputfile

This should have comparable performance in bash, as (....) starts a subshell, but echo is 'inlined' - bash does not run /bin/echo, but does the echo by itself.

It might even be faster because it involves no exec().

This style is even more useful if you want to use output from another command somewhere in the text.

0

精彩评论

暂无评论...
验证码 换一张
取 消