Add new line character at the end of file in bash

2020-05-17 04:25发布

How can I use bash command line to add new line character at the end of file named file.txt. I tried use echo but it's not right. Can you show me how can I do it?

5条回答
相关推荐>>
2楼-- · 2020-05-17 04:33
echo "" >> file.txt

Adds a newline character at the end

查看更多
不美不萌又怎样
3楼-- · 2020-05-17 04:50

This works on centos the empty string above didn't.

sed -i -e '$a\' file
查看更多
放荡不羁爱自由
4楼-- · 2020-05-17 04:50

GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14)

echo \ >> file.txt
查看更多
劳资没心,怎么记你
5楼-- · 2020-05-17 04:52

Use Vi which automatically creates EOL at EOF on file save.

For example:

vi -escwq foo.txt

or:

ex -scwq foo.txt

To correct multiple files, check: How to fix 'No newline at end of file' for lots of files? at SO

查看更多
地球回转人心会变
6楼-- · 2020-05-17 04:54

With sed:

sed -i '' -e '$a\' file.txt

with echo:

echo  >> file.txt
查看更多
登录 后发表回答