bash 在bash中的文件末尾添加新行字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23055831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:47:31  来源:igfitidea点击:

Add new line character at the end of file in bash

bashfilecommandnewline

提问by Do Thanh Tung

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

如何使用 bash 命令行在名为file.txt. 我试过用,echo但不对。你能告诉我我该怎么做吗?

回答by Ashkan

echo "" >> file.txt

Adds a newline character at the end

在末尾添加一个换行符

回答by MLSC

With sed:

sed

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

with echo:

echo

echo  >> file.txt

回答by kenorb

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

使用 Vi,它会在文件保存时自动在 EOF 处创建 EOL。

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

要更正多个文件,请检查:如何修复许多文件的“文件末尾无换行符”?在 SO

回答by jusopi

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

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

echo \ >> file.txt

回答by GarethReid

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

这适用于centos,上面的空字符串没有。

sed -i -e '$a\' file